3
votes

I had dozens of Elm 0.17 programs for experimenting with the language. The scripts each featured simple assertions, and the program would execute and run all the assertions, and the program would succeed (process exit code 0) or fail (process exit code not 0). Here is an example:

module Main exposing (..)

import ElmTest exposing (runSuite, suite, defaultTest, assertEqual)
import List exposing (map)

boss = { name = "Alice", salary = 200000 }
worker = { name = "Bob", salary = 50000, supervisor = boss }
newWorker = { worker | name = "Carol" }

payrollTax : { a | salary : Float } -> Float
payrollTax { salary } = salary * 0.15


main = runSuite <| suite "Exploring records" <| map defaultTest
    [ assertEqual "Alice" (.name boss)
    , assertEqual boss worker.supervisor
    , assertEqual 7500.0 (payrollTax worker)
    , assertEqual boss newWorker.supervisor
    ]

Back in Elm 0.17, I would just run

$ elm make records.elm --output tests.js && node tests.js

And great, I'd see if the tests worked!

I finally decided to run elm upgrade to get to 0.18 and now (no real surprise) all those things are broken. Apparently, the whole concept of elm-test is now completely changed! Of course, elm upgrade did a nice job of updating my elm-package.json

    "dependencies": {
        "elm-community/elm-test": "4.0.0 <= v < 5.0.0",

but the new module does not have ElmTest and runSuite and friends anymore. I've read the docs and see that we now have Test, Expect, and Fuzz, which is fine. But it seems that now we are supposed to run

elm test init

and then

elm test

to discover and run all the tests. Instead of writing an app with main, we're now, I think, supposed to put tests in a test directory as modules exposing a Test object. Then we run elm test.

But I am not trying to run tests! I just want to write little scripts that make assertions about language features. I've done this in a couple dozen other languages and I find it a great way to learn.

How, then, in Elm 0.18, can I make a program that I can run with elm make or similar, that runs from the command line and has assertions? (I realize Elm is not for command line programs, but this was easy to do in 0.17, so how can I do something similar in 0.18?

1
I suggest you install an earlier version of elm-test. v4 added the new syntax but is really only in beta. v3 still works with 0.18 and has the hooks you want - Simon H
Thanks, though the hook I am looking for is something similar to elm-test version 1.1, which had the module ElmTest with the function runSuite : Test -> Program Never. I can't find any function, even in elm-test 3.0.0, that produces a Program object. Thus I'm unable to run tests from a main. Am I missing something? Is there any way to produce a Program object and still use assertions? - Ray Toal
whoops - apologies - I was writing from memory and see it is now run emit in v3 - Simon H
Who knew? I never heard about this, thanks. Looking through the Elm source code I did find the TestProgram type used in github.com/elm-lang/core/blob/master/tests/Main.elm but AFAICT this appears nowhere in public documentation. Wonder if we are not supposed to use this? - Ray Toal

1 Answers

2
votes

You were using an old version of that library. runSuite, assertions, etc are from 1.0.0 version and the current version is 4.0.0+.

I recomend you read the Upgrading guide from 0.17 to 0.18 and the docs for elm-community/elm-test latest

I hope it helps.