How do you test a TEA `update` function?

180 views
Skip to first unread message

Francesco Orsenigo

unread,
Nov 7, 2016, 1:44:36 AM11/7/16
to Elm Discuss
How do you write unit tests to ensure that an `update : Msg -> Model -> ( Model, Cmd Msg )` function is producing the correct commands?
What if the function is supposed to produce several commands batched together?

Same thing for a `view : Model -> Html Msg`.
Suppose I want to test whether, given a particular model, it will display the correct number of list items.
Do people write tests for this?
Right now the only way to write this kind of tests I can think of is creating the whole html tree as I expect it to be rendered, and comparing it via == with the function output.

Simon

unread,
Nov 7, 2016, 2:28:19 AM11/7/16
to Elm Discuss
I've often worried about the commands bit.
For the view side, you need to test that you are deriving the right data to render, while it is Evan that needs to test that, given certain data, expected DOM elements get produced?

Austin Bingham

unread,
Nov 7, 2016, 3:51:56 AM11/7/16
to Elm Discuss
I asked more or less the same question about a month ago and got no responses:


The only practical answer right now seems to be to use elm-testable, though I haven't actually tried it myself.

This feels like a real blind-spot in the elm ecosystem. The generation and handling of Cmds is central to a properly functioning app, but there doesn't seem to be any first-class way to test e.g. Update without driving the entire app from the top-level. 

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon

unread,
Nov 7, 2016, 3:54:53 AM11/7/16
to Elm Discuss
... which is ironic given all emphasis on 'effects as data'....

Oliver Searle-Barnes

unread,
Nov 7, 2016, 7:34:18 AM11/7/16
to Elm Discuss
This is an area I've been a little uncomfortable with. There seems two be a couple of options though:

1) https://github.com/avh4/elm-testable - I haven't used it but it seems that it provides replacement commands that you can run assertions against. This approach seems limited as it only solves the problem for a predefined set of commands.

2) Another approach, which I also haven't tried yet: Instead of returning commands from your update, model the effects yourself and only convert them into commands at the last minute when returning them from your main/update. For instance you might return MyEffects.request(endpoint, verb, payload, GoodResponse, BadResponse). This would allow you to just use assertEquals on the effects returned from your update. Something that would need to be taken care of though is a replacement for Cmd.map. 

Mark Hamburg

unread,
Nov 7, 2016, 12:40:24 PM11/7/16
to elm-d...@googlegroups.com
See, for example, here:


An approach in which one waits "longer" to convert requests to commands appears to be more readily testable. (That said, I haven't taken to writing unit tests yet for this code and while one can compare for equality, command like entities will tend to involve tagger functions which may result in fatal runtime errors in those equality checks.)

Mark
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss+unsubscribe@googlegroups.com.

Francesco Orsenigo

unread,
Nov 8, 2016, 4:31:08 PM11/8/16
to Elm Discuss

Ok, but is it justified to make your code more complicated just to enable testing?

Mark Hamburg

unread,
Nov 9, 2016, 11:23:50 AM11/9/16
to elm-d...@googlegroups.com
We didn't do it to enable testing. We did it because it also regularizes a lot of other module interactions. Improving testing is a bonus.

Mark

On Nov 8, 2016, at 1:31 PM, Francesco Orsenigo <francesco...@gmail.com> wrote:


Ok, but is it justified to make your code more complicated just to enable testing?

--

Francesco Orsenigo

unread,
Nov 9, 2016, 4:51:57 PM11/9/16
to elm-d...@googlegroups.com
Fair enough!
> You received this message because you are subscribed to a topic in the
> Google Groups "Elm Discuss" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/elm-discuss/IYp-bI93AnY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
Reply all
Reply to author
Forward
0 new messages