I’m really unsure on how to test Tasks.
If for example I have something superdumb like:
presence : String -> Task String String
presence value =
if value == "" then Task.fail "is not present"
else Task.succeed value
how can I unit test it using https://github.com/elm-explorations/test ?
Is this very difficult or I asked a silly question?
It seems elm-test doesn’t support testing Tasks.
Some related links I’ve found:
- StackOverflow questions from 2016 (can be partially out of date)
- Old packages which supported testing Tasks, but unfortunately don’t support Elm 0.19
I just watched a Twitch stream of avh4 where he mentions shortly at end that it’s been planned to add testing of Commands and Tasks to elm-explorations/test, basically what he did earlier in avh4/elm-testable.
So it seems there isn’t a way to do this currently (at least I havn’t found any), but it might be added later.
 at 2:13:33 in https://www.twitch.tv/videos/335236523
I’d encourage you to think less about unit-testing individual functions and more about using tests to describe the behavior of your program at a higher level.
For example, instead of testing that some particular function returns the right
Task, maybe you want to test that your program displays some information when some response to an Http request is received. The implementation might involve a function that returns a
Task (if you’re using
Http.toTask) but that’s just a detail. If you could write a test that describes how your software should work at a high level without knowledge of such details, then you’d (1) be sure that your software does the right thing and (2) give yourself freedom to refactor your code later without changing your tests. Maybe tomorrow you decide that you no longer need to use
Http.toTask. If you write tests that describe behavior instead of implementation details, you could make that change, run your tests and – if they pass – feel confident that your software still has all the behavior you’ve described in those tests.
I know of two ways to do this today with Elm.
Run your Elm program and write a test that interacts with your program purely through the DOM – ie treat your Elm program like a black box from the perspective of your tests. You could do this in several ways. Start your app so that you can visit it with a web browser; write a test (in Jasmine or Mocha/Chai) that drives the web browser (with Pupeteer) and makes relevent expectations. Or, run your tests (written in Jasmine or Mocha/Chai) with Karma so you have access to a DOM, mount your Elm Program before each test, then use DOM functions to exercise it and make expectations.
You could use Elmer to write tests in Elm that describe the behavior of your app. (Disclaimer: I wrote Elmer.) Elmer allows you to stub
Sub values (and ports) so that you can simulate conditions under which you want to describe the behavior of your program.
Here’s a link to a section of the Elmer README that describes how to test tasks with Elmer.
There may be other approaches beyond these two, but I’ve had good success with both.
I’ve had success with a strategy similar to the one @hkgumbs describes here: https://kofi.sexy/blog/testing-cmds-in-elm/
This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.