Uploaded by
Published on

purescript-spec-mocha is a runner and reporter for purescript-spec that run tests and reports the results using the Mocha interface (describe, it etc). This enables you to use purescript-spec together with mocha and karma, and thus run tests in web browsers, as well as NodeJS.


spago install spec-mocha
module Main where

import Prelude
import Control.Monad.Eff (Eff)
import Test.Spec (SpecEffects, describe, it)
import Test.Spec.Assertions (shouldEqual)
import Test.Spec.Mocha (runMocha)

main :: Effect Unit
main = runMocha do
  describe "your feature" do
    it "works" $
      (1 + 1) `shouldEqual` 2

See test/Main.purs for a more detailed example. The package.json scripts in this repo, show a number of usage patterns further detailed below. Note that the example tests contain 2 passing tests, a pending test and 2 failing tests, to demonstrate both success and failure.

Usage with bundled Purescript

You can run yarn test:node or npm run test:node in this repo to see an example.

If you bundle your compiled PureScript it can be run with mocha bundle.js or using Karma and karma-mocha.

spago bundle-app --main Test.Main --to bundle.js
mocha bundle.js

Usage in the browser

To run mocha tests in the browser, you can run yarn test:browser or npm run test:browser in this repo to see an example using the test/browser/index.html file.

<script src="../../output/test.js"></script>

It's also possible to bundle the test as a module in which case you'll need to use type="module":

<script type="module" src="./index_module.js"></script>

and to import the test module as shown in the test/browser/index_module.js file:

import { main } from "../../output/test_module.js";


Running npm run test:watch in one terminal window and npm run test:browser in another will watch purescript source and tests files and automatically run node and browser tests.

Usage with headless browser

You can run yarn test:headless or npm run test:headless in this repo to see an example using the test/index.html file together with mocha-headless-chrome. Note that we need to disable-web-security in chromium to allow cross-origin requests.

API Documentation

See docs on Pursuit.


If you have any issues or possible improvements please file them as GitHub Issues. Pull requests requests are encouraged.

Running Tests

This project's tests include some failures to test the Mocha integration. Thus, use instead of spago test to check that everything is all right.


MIT License.