| Copyright | (C) 2016-2026 David M. Johnson |
|---|---|
| License | BSD3-style (see the file LICENSE) |
| Maintainer | David M. Johnson <code@dmj.io> |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Miso.Test
Contents
Description
Overview
Miso.Test is a lightweight hspec-style testing framework for miso
Component integration tests. It runs in the browser (or a
JSDOM environment) via
Playwright, giving each test full access to
the live DOM.
Tests are written as , which means they can call
any StateT TestState IOIO action — including miso DOM operations and FFI calls — directly.
Each it block is timed and its result is printed with coloured output.
After all tests run, runTests prints a summary and calls
or exitSuccess so the process
exit code reflects the test outcome.exitFailure
Quick start
import Miso.Test import Miso.Runtime.Internal (components) import Data.IORef (writeIORef) import qualified Data.IntMap.Strict as IM main :: IO () main =runTests$ dodescribe"Arithmetic" $ doit"2 + 2 = 4" $ (2 + 2) `shouldBe` (4 :: Int)it"3 /= 4" $ (3 :: Int) `shouldNotBe` 4describe"Component" $beforeEach(writeIORef components IM.empty) $ doit"mounts without error" $ do -- mount a component and assert on DOM state pure ()
Combinators
describe— group related tests under a label.it— declare a single named test; times it and records pass/fail.beforeEach— run anIOaction before everyitin the wrapped block.afterEach— run anIOaction after everyitin the wrapped block.
Assertion primitives
x `— assertshouldBe` yx == y; pretty-prints a diff on failure.x `— assertshouldNotBe` yx /= y.x `— assert that the predicateshouldSatisfy` pp xholds.— assertexpectf x yf x y; the most general form.
Multiple assertions within a single it block are all evaluated; the
block is marked as failed if any assertion fails.
Running tests
is the entry point. It executes the
test tree, prints per-test results and a final summary (pass count,
fail count, total tests, total duration, and runTests :: Test a -> IO ()expect() call count),
then exits:
— all tests passed.exitSuccess— one or more tests failed.exitFailure
When compiled with -DJSDOM, runTests additionally calls
globalThis.initJSDOM() before running any tests, enabling headless
DOM testing outside a real browser.
Utilities
choosemin max— returns a uniformly randomIntin[min, max)using the JSgetRandomNumberglobal. Useful for property-style tests that need random inputs.
Types
— the test monad.Testa =StateTTestStateIO aTestState— internal state tracking per-test timing, pass/fail counts, before/after hooks, and error messages.
See also
- Miso.Runtime.Internal —
componentsIORef for resetting state between tests - Miso.Random —
newStdGen,replicateRMfor random values - Miso — the component and effect API under test
Synopsis
- describe :: MisoString -> Test () -> Test ()
- it :: MisoString -> Test () -> Test ()
- expect :: (Eq a, Show a) => (a -> a -> Bool) -> a -> a -> Test ()
- beforeEach :: IO () -> Test () -> Test ()
- afterEach :: IO () -> Test () -> Test ()
- shouldBe :: (Show a, Eq a) => a -> a -> Test ()
- shouldSatisfy :: (Eq a, Show a) => a -> (a -> Bool) -> Test ()
- shouldNotBe :: (Show a, Eq a) => a -> a -> Test ()
- runTests :: Test a -> IO ()
- choose :: Int -> Int -> IO Int
- type Test a = StateT TestState IO a
- data TestState
Test Combinators
Arguments
| :: MisoString | Description of test group |
| -> Test () | Group of tests to run |
| -> Test () |
Used to group a bunch of expectations using it. Testing out
will include the test description in its output.
Arguments
| :: MisoString | Name of test to execute |
| -> Test () | Test holding multiple expectations |
| -> Test () |
Used to make multiple expectations using shouldBe / shouldNotBe.
Arguments
| :: (Eq a, Show a) | |
| => (a -> a -> Bool) | Comparison predicate; the test passes when this returns |
| -> a | Actual value (shown in failure output as Received) |
| -> a | Expected value (shown in failure output as Expecting) |
| -> Test () |
Primitive for performing expectations in an it block.
Performs an expectation in an it block.
Arguments
| :: (Eq a, Show a) | |
| => a | Actual value to test |
| -> (a -> Bool) | Predicate that must hold for the assertion to pass |
| -> Test () |
Primitive for performing expectations in an it block.
Executes a block of tests in describe blocks.
Utils
Return a random integer between the first two provided [min, max)
The maximum is exclusive and the minimum is inclusive