elm-community/elm-test/4.1.1

NOTE: this package is not compatible with Elm 0.19.1

Test.Runner

This is an "experts only" module that exposes functions needed to run and display tests. A typical user will use an existing runner library for Node or the browser, which is implemented using this interface. A list of these runners can be found in the README.

Runner

type alias Runner =
{ run : () -> List Expectation
, labels : List String
}

A function which, when evaluated, produces a list of expectations. Also a list of labels which apply to this outcome.

= Plain (List Runner)
| Only (List Runner)
| Skipping (List Runner)
| Invalid String
fromTest : Int -> Seed -> Test -> SeededRunners

Convert a Test into SeededRunners.

In order to run any fuzz tests that the Test may have, it requires a default run count as well as an initial Random.Pcg.Seed. 100 is a good run count. To obtain a good random seed, pass a random 32-bit integer to Random.Pcg.initialSeed. You can obtain such an integer by running Math.floor(Math.random()*0xFFFFFFFF) in Node. It's typically fine to hard-code this value into your Elm code; it's easy and makes your tests reproducible.

Expectations

Expectation
-> Maybe
{ given : Maybe String
, message : String
}

Return Nothing if the given Expectation is a pass.

If it is a fail, return a record containing the failure message, along with the given inputs if it was a fuzz test. (If no inputs were involved, the record's given field will be Nothing).

For example, if a fuzz test generates random integers, this might return { message = "it was supposed to be positive", given = "-1" }

getFailure (Expect.fail "this failed")
-- Just { message = "this failed", given = "" }


getFailure (Expect.pass)
-- Nothing
isTodo : Expectation -> Bool

Determine if an expectation was created by a call to Test.todo. Runners may treat these tests differently in their output.

Formatting

formatLabels : (String -> format) -> (String -> format) -> List String -> List format

A standard way to format descriptions and test labels, to keep things consistent across test runner implementations.

The HTML, Node, String, and Log runners all use this.

What it does:

  • drop any labels that are empty strings
  • format the first label differently from the others
  • reverse the resulting list

Example:

[ "the actual test that failed"
, "nested description failure"
, "top-level description failure"
]
|> formatLabels ((++) "↓ ") ((++) "✗ ")


{-
[ "↓ top-level description failure"
, "↓ nested description failure"
, "✗ the actual test that failed"
]
-}

Fuzzers

These functions give you the ability to run fuzzers separate of running fuzz tests.

type Shrinkable a

A Shrinkable a is an opaque type that allows you to obtain a value of type a that is smaller than the one you've previously obtained.

fuzz : Fuzzer a -> Generator ( a, Shrinkable a )

Given a fuzzer, return a random generator to produce a value and a Shrinkable. The value is what a fuzz test would have received as input.

shrink : Bool -> Shrinkable a -> Maybe ( a, Shrinkable a )

Given a Shrinkable, attempt to shrink the value further. Pass False to indicate that the last value you've seen (from either fuzz or this function) caused the test to fail. This will attempt to find a smaller value. Pass True if the test passed. If you have already seen a failure, this will attempt to shrink that failure in another way. In both cases, it may be impossible to shrink the value, represented by Nothing.