Skip to content

Commit a54ae10

Browse files
authored
Improve browser and headless test documentation and examples (#20)
1 parent ae9bf9f commit a54ae10

File tree

10 files changed

+8936
-2181
lines changed

10 files changed

+8936
-2181
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ jobs:
2424
2525
- name: Build and test project
2626
run: ./run_tests.sh
27+
28+
- name: Build and test in headless browser
29+
run: ./run_headless_tests.sh

README.md

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ main = runMocha do
2828
(1 + 1) `shouldEqual` 2
2929
```
3030

31+
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.
32+
3133
### Usage with bundled Purescript
34+
35+
You can run `yarn test:node` or `npm run test:node` in this repo to see an example.
36+
3237
If you bundle your compiled PureScript it can be run with `mocha bundle.js` or
3338
using Karma and [karma-mocha](https://github.com/karma-runner/karma-mocha).
3439

@@ -38,52 +43,37 @@ mocha bundle.js
3843
```
3944

4045
### Usage in the browser
41-
If you want to mix in Purescript tests with existing Javascript (or
42-
Coffeescript) Mocha tests running in the browser, you'll need to import the file
43-
and call the function exported by your Purescript test. E.g. combining the
44-
example from [Running Mocha in the
45-
Browser](https://mochajs.org/#running-mocha-in-the-browser) with the above
46-
Purscript spec, you'll need:
46+
47+
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.
4748

4849
```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-
...
50+
<script>mocha.setup("bdd");</script>
51+
<script src="../../output/test.js"></script>
52+
<script>mocha.run();</script>
5953
```
6054

61-
```javascript
62-
// all_tests.js
63-
require('test.array.js'); // Javascript specs load when the the file is parsed.
64-
require('test.object.js');
65-
require('test.xhr.js');
55+
It's also possible to bundle the test as a module in which case you'll need to use `type="module"`:
6656

67-
{main} = require('my_purescript_spec');
68-
main(); // Purescript specs load when the function is called.
57+
```html
58+
<script type="module" src="./index_module.js"></script>
6959
```
7060

71-
### Usage with Spago and Parcel
72-
73-
With `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-
...
61+
and to import the test module as shown in the `test/browser/index_module.js` file:
62+
63+
```javascript
64+
import { main } from "../../output/test_module.js";
65+
66+
mocha.setup("bdd");
67+
main();
68+
mocha.run();
8369
```
8470

8571
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.
8672

73+
### Usage with headless browser
74+
75+
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.
76+
8777
## API Documentation
8878

8979
See [docs on Pursuit](https://pursuit.purescript.org/packages/purescript-spec-mocha).

0 commit comments

Comments
 (0)