git.iko.soy/purescript-spec-mocha
on master help sign in
105 lines 3.2 kB history raw
1# purescript-spec-mocha
3purescript-spec-mocha is a runner and reporter for
4[purescript-spec](https://github.com/owickstrom/purescript-spec) that run tests
5and reports the results using the Mocha interface (`describe`, `it` etc). This
6enables you to use purescript-spec together with `mocha` and `karma`, and thus
7run tests in web browsers, as well as NodeJS.
9## Usage
11```bash
12bower install purescript-spec-mocha
13```
15```purescript
16module Main where
18import Prelude
19import Control.Monad.Eff (Eff)
20import Test.Spec (SpecEffects, describe, it)
21import Test.Spec.Assertions (shouldEqual)
22import Test.Spec.Mocha (MOCHA, runMocha)
24main :: Eff (SpecEffects (mocha :: MOCHA)) Unit
25main = runMocha do
26 describe "your feature" do
27 it "works" $
28 (1 + 1) `shouldEqual` 2
29```
31### Usage with bundled Purescript
32If you bundle your compiled PureScript it can be run with `mocha bundle.js` or
33using Karma and [karma-mocha](https://github.com/karma-runner/karma-mocha).
35```bash
36pulp browserify -I test --main Test.Main > bundle.js
37mocha bundle.js
38```
40### Usage in the browser
41If you want to mix in Purescript tests with existing Javascript (or
42Coffeescript) Mocha tests running in the browser, you'll need to import the file
43and call the function exported by your Purescript test. E.g. combining the
44example from [Running Mocha in the
45Browser](https://mochajs.org/#running-mocha-in-the-browser) with the above
46Purscript spec, you'll need:
48```html
49<!-- test/index.html -->
50...
51 <script>mocha.setup('bdd')</script>
52 <script src="all_tests.js"></script>
53 <script>
54 mocha.checkLeaks();
55 mocha.globals(['jQuery']);
56 mocha.run();
57 </script>
58...
59```
61```javascript
62// all_tests.js
63require('test.array.js'); // Javascript specs load when the the file is parsed.
64require('test.object.js');
65require('test.xhr.js');
67{main} = require('my_purescript_spec');
68main(); // Purescript specs load when the function is called.
69```
71### Usage with Spago and Parcel
73With `spago` and `parcel-bundler`, and the above `test/index.html` you can build tests, and run them on node and browsers with the following entries in your `package.json`
74```json
75...
76 "scripts": {
77 "test:build": "spago bundle-app --main Test.Main --to ./output/test.js",
78 "test:watch": "spago bundle-app --watch --main Test.Main --to ./output/test.js --then \"npm run -s test:node\"",
79 "test:node": "mocha ./output/test.js",
80 "test:browser": "parcel test/index.html --open"
81 },
82...
83```
85Running `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.
87## API Documentation
89See [docs on Pursuit](https://pursuit.purescript.org/packages/purescript-spec-mocha).
91## Contribute
93If you have any issues or possible improvements please file them as
94[GitHub Issues](https://github.com/owickstrom/purescript-spec-mocha/issues).
95Pull requests requests are encouraged.
97### Running Tests
99This project's tests include some failures to test the Mocha
100integration. Thus, use `run_tests.sh` instead of `pulp test` to check
101that everything is all right.
103## License
105[MIT License](LICENSE.md).