elm-explorations / elm-explorations/test

Add lazy version of `Expect.onFail`

Open
#239 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Elm
Stars
244
Forks
40
Avg merge
1d 23h
Merged PRs (30d)
2

Description

`Expect.onFail` allows users to replace the failure message for a given test failure. From the documentation:

```elm
"something"
|> Expect.equal "something else"
|> Expect.onFail "thought those two strings would be the same"
```

The problem with this is that failure message is computed eagerly and not lazily.
In the `elm-review` testing library, some of the assertions compute a fairly complex failure message which can be slow to generate. Being able to do it lazily can make the tests run much faster.

In practice, this is already possible through a custom function like this:

```elm
onFailLazy : (() -> String) -> Expectation -> Expectation
onFailLazy message expectation =
case Test.Runner.getFailureReason expectation of
Just _ ->
expectation |> Expect.onFail (message ())

Nothing ->
expectation
```

but this is very tricky to figure out by yourself, as `Test.Runner.getFailureReason` is necessary and it isn't showcased much (for good reasons).

I would like to propose that this function makes it in the library (maybe under a different name, I'm not attached to `onFailLazy` at all).

---

When mentioning this on Slack, @Janiczek mentioned that there are also places where the initial error message would be good to keep. In which case, we could make a similar function to the above available where the failure reason received from `Test.Runner.getFailureReason` is not ignored (or just have one function for both) (cc @edkelly303 who first mentioned it AFAIK)

```elm
onFailure : (String -> String) -> Expectation -> Expectation
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.