elm-test

repository·master·Indexed 42 days ago

https://github.com/elm-explorations/test

A library for writing unit and fuzz tests in Elm. It provides primitives for defining test suites, assertions, and property-based tests using functions like describe, test, Expect, and fuzz, which are executed by external CLI runners such as node-test-runner or elm-test-rs.

Tokens
1.8K
Snippets
3
Records
8
Agent score
4%

What's inside elm-test

  1. Manage test execution with skip, only, and todo

    master

    During development, you can control which tests are executed using three functions. Note that using these results in an incomplete test run, which CI systems typically report as a failure to prevent accidentally committing a gutted test suite.

    • only: Marks a suite so that only the tests within that block are run. Other tests in the suite are ignored.
    • skip: Skips a specific test, even if it is inside an only block.
    • todo: Marks a test as a placeholder for future implementation.
    wipSuite : Test
    wipSuite =
        describe "skip, only, and todo"
            [ only <|
                describe "Marking this test as `only` means no other tests will be run!"
                    [ test "This test will be run" <| 
                        \_ -> 1 + 1 |> Expect.equal 2
                    , skip <| test "This test will be skipped, even though it's in an `only`!" <| 
                        \_ -> 2 + 3 |> Expect.equal 4
                    ]
            , test "This test will be skipped because it has no `only`" <| 
                \_ -> "left" |> Expect.equal "right"
            , todo "Make sure all splines are reticulated"
            ]
  2. How to use elm-test benchmarks

    master

    The benchmarks are intended to help developers understand the performance impact of changes made to the elm-test codebase.

    Important Considerations:

    • A performance increase in these benchmarks may not translate directly to a proportional increase in real-world test speed.
    • These benchmarks do not account for the execution time of the test bodies themselves, which varies significantly between different test suites.
    • For a more accurate assessment of how changes affect actual test run times, you should run real test suites both with and without your changes.
  3. Quick Start with elm-test

    master

    To write tests in Elm, use the elm-test package to define Test values. You can create hierarchical test suites using describe, write individual unit tests with test, assert results using Expect, and perform property-based testing with fuzz.

    suite : Test
    suite =
        describe "The String module"
            [ describe "String.reverse"
                [ test "has no effect on a palindrome" <|
                    \_ ->
                        let
                            palindrome = "hannah"
                        in
                            Expect.equal palindrome (String.reverse palindrome)
                , test "reverses a known string" <|
                    \_ ->
                        "ABCDEFG"
                            |> String.reverse
                            |> Expect.equal "GFEDCBA"
                , fuzz string "restores the original string if you run it again" <|
                    \randomlyGeneratedString ->
                        randomlyGeneratedString
                            |> String.reverse
                            |> String.reverse
                            |> Expect.equal randomlyGeneratedString
                ]
            ]
  4. Best practices for effective Elm testing

    master

    When writing tests for Elm, consider these strategies:

    • Prefer Fuzzing: Use fuzz to test properties of your code rather than just specific unit cases. For union types, map over all possible values.
    • TDD Approach: Treat compiler errors as 'red' tests. Write the tests for functions that don't exist yet to drive your API design.
    • API Design: If an API is hard to test, it is likely hard to use. Use your tests as your first client.
    • Test Boundaries: Only test the exposed interface. Do not attempt to import unexposed functions just for testing purposes.
    • Small & Focused: Keep tests small. Move setup code into reusable functions or custom fuzzers.
    • Regression Testing: For simple functions, copying the implementation into the test can serve as a useful regression check.
    • Documentation Integration: Consider using elm-verify-examples to turn your documentation examples into automated unit tests.
  5. How to run elm-test suites

    master

    The elm-test package only defines tests (as Test values). To execute them in your terminal, you must use a CLI test runner. The two most popular runners are:

    • rtfeldman/node-test-runner (often referred to as the elm-test CLI)
    • mpizenberg/elm-test-rs

    To get started with the Node runner, you can run elm-test init to generate example tests.

  6. Benchmark complete test suites with modified elm-test

    master

    To benchmark how your modified version of elm-test performs against a real-world test suite, follow these steps. This method requires your modified version to be backwards compatible with the version currently used by the test suite.

    1. Modify the test suite's elm-package.json:
      • Remove the existing dependency on elm-test.
      • Add the dependencies of elm-test as direct dependencies of the test suite.
      • Add the path to your modified elm-test source directory to the source-directories array (e.g., "source-directories": ["/<projects-dir>/elm-test/src"]).
    2. Trigger compilation: Run elm-test once to ensure everything compiles correctly.
    3. Run the benchmark: Execute elm-test using your preferred benchmarking tool (such as bench).
  7. Testing Elm web applications and views

    master

    When testing web applications or view packages, follow these specific techniques:

    • Module Imports: Avoid importing your Main module directly. Instead, structure your code so you can import the specific logic modules you need to test.
    • View Testing: Use Test.Html.Query, Test.Html.Selector, and Test.Html.Event to test your HTML views.
    • Integration/E2E Testing: Since there is no native Elm solution for end-to-end testing, use external tools like Cypress or Capybara.