How are you formating your elm test code?

Tests are syntactically checked code but indenting can mean the interesting stuff is displayed ‘off to the right’.

simple : Test
simple =
    describe "leap year tests"
        [ test "isn't a leap year" <|
            \_ ->
                isLeapYear 2001 |> Expect.equal False
        , test "is a leap year" <|
            |_ ->
                isLeapYear 2000 |> Expect.equal True
        ]
        

Is anyone using a different format?

I’m using the same. Except that instead of \_ -> I have \() ->. I like to be explicit about parameters.
(Then if I turn the unit test into a fuzzer test then I get a compiler error to remind me to use the fuzz value)

5 Likes

I don’t think you’ll be able to reduce the indentation, but formatting-wise I do ↓

simple : Test
simple =
    Test.describe "leap year"
        [ test "no"
            (\() ->
                2001
                    |> isLeapYear
                    |> Expect.equal False
            )
        , test "yes"
            (\() ->
                2000
                    |> isLeapYear
                    |> Expect.equal True
            )
        ]
  • () gives you the guarantee that no values will arrive there, which is helpful to remind you if you for example change to Test.fuzz. (as jfmengels wrote)
  • <| → paren application, op data |> expectdata |> op |> expect for reasons of simplicity, consistency:
    • Pipe data before the function: food |> op ...
    • Feed arguments after the function: ... |> opWith (a ...) (b ...)
  • Test.describe just to remove names exposed with implied context. Maybe describe is something a test tests or uses as a variable name in the future?
  • no repeating in tests what you already described
1 Like

I format my tests like this:

5 Likes

That looks rather excellent, Rupert
A tiny bit of repetition for a clean syntax and much code close to the left-margin

If indentation is of concern you would gain one more by piping the list into describe. :wink:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.