Elm Language - Fine-Tuning:

COSC4315 TT
3 min readMar 20, 2019

--

Testing & Debugging

Part 1 and Part 2 of series on Elm

Introduction

Testing and debugging in Elm is straightforward. The tools explored here, are the compiler, the debug library, and the elm test package. To start with, there are no run-time errors in Elm. These are errors that occur during program execution. For example, Elm takes into consideration the possibility of incorrect input with the implementation of custom types. This rids Elm of many of the errors seen in other languages. Furthermore, functions in Elm are pure functions, meaning that they return the same value given the same arguments. These sorts of functions do not cause side effects. Side effects occur when a function performs anything else other than returning a calculated value. Obviously, pure functions make a program easier to debug. These characteristics, coupled with efficient compiler error detection, immutable data, and useful packages, allow for painless debugging and testing.

Compiler Error Detection

The Elm compiler is quite capable of detecting many common errors. One of the major features of Elm that is frequently praised by developers, is its compiler error detection. Compiler error messages in Elm are user-friendly. These appear in the terminal or in the text editor if it is being used instead. They often contain suggestions or hints that are quite useful.

useful compiler hint

Testing

Unit testing which focuses on testing each unit(component,class, content object), and fuzz testing which inputs large amounts of random data(fuzz) to the program in an attempt to make it crash, can be implemented in Elm using the elm test package.

To install enter npm install -g elm-test from your terminal. Next, cd into the project’s root directory. After, enter elm-test init from your terminal. This will create a tests directory with some files inside. We can open Example.elm to write a test. By entering elm-test from the terminal, we can see the result.

editing Example.elm
3+3 does in fact equal the expected value which is 6

Debugging

The Debug library is part of the core Elm core package. One of its functions is called log.

Another function in the debug library is called crash. As the name implies, this function terminates the program with an error message. This can be used when a case expression is unfinished. The error will point to the related module name and line number.

The quickest way to debug in elm is through the console in your web browser. Once you configure the launch.json settings to properly run the file, go into your terminal or command prompt and enter “elm reactor” (without the parentheses). Then go find your elm file that you wanted to debug and run it. Right click on the screen and click inspect, then click the console tab to keep track of your program. A video to show how the debugger is used can be found here:

https://youtu.be/vYvsbei2BX0

Time Travel Debug Tool

One of the greatest debug features included with the Elm language is the ability to record a snapshot of the session history and updates made to the model using the Time Travel debug tool. The process for this is shown in the video below.

Time Travel Debugger

References:

https://elm-lang.org

https://elmprogramming.com/

--

--