Skip to content

Commit cb47925

Browse files
TG199mark-wiemer
andauthored
docs: migrate programmatic usage to docs, development content to DEVELOPMENT.md (#5464)
Co-authored-by: Mark Wiemer <[email protected]>
1 parent 9a70533 commit cb47925

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

.github/DEVELOPMENT.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,32 @@ If you are having trouble, don't be afraid to [ask for help](./CONTRIBUTING.md#
1616
- Do not use `yarn install` or `pnpm install`.
1717
- Some optional dependencies may fail; you can safely ignore these unless you are trying to build the documentation.
1818
- If you're sick of seeing the failures, run `npm install --ignore-scripts`.
19+
20+
## Developing Mocha
21+
22+
When you contribute to Mocha, you will probably want to try to run your changes on the test suite of another project. You can (and should) run the test suite of Mocha itself before committing, but also confirming that your changes give the expected result on another project.
23+
24+
For example, [WebSocket.io](https://github.com/LearnBoost/websocket.io/):
25+
26+
$ git clone https://github.com/LearnBoost/websocket.io.git
27+
28+
Retrieve websocket.io's dependencies, which will include the stable version of Mocha:
29+
30+
$ cd websocket.io/
31+
$ npm install
32+
33+
Replace the Mocha dependency by the current git repository:
34+
35+
$ cd node_modules/
36+
$ mv mocha/ mocha.save
37+
$ git clone https://github.com/mochajs/mocha.git
38+
39+
Install Mocha's dependencies for the development version:
40+
41+
$ cd mocha
42+
$ npm install
43+
44+
Run websocket.io's test suite using the development version you just installed:
45+
46+
$ cd ../..
47+
$ ./node_modules/.bin/mocha

docs-next/astro.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ export default defineConfig({
124124
label: "Node.js native ESM support",
125125
slug: "explainers/nodejs-native-esm-support",
126126
},
127+
{
128+
label: "Programmatic usage",
129+
slug: "explainers/programmatic-usage",
130+
},
127131
{
128132
label: "Related tools",
129133
slug: "explainers/related-tools",
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
description: Using Mocha programmatically
3+
title: Programmatic usage
4+
---
5+
6+
There are a lot of reasons why you might want to automate running the tests using Mocha. Using the command-line can run into some problems if you want to load specific files, for example.
7+
8+
Here is an example of using Mocha programmatically:
9+
10+
```javascript
11+
var Mocha = require("mocha"),
12+
fs = require("fs"),
13+
path = require("path");
14+
15+
// Instantiate a Mocha instance.
16+
var mocha = new Mocha();
17+
18+
var testDir = "some/dir/test";
19+
20+
// Add each .js file to the mocha instance
21+
fs.readdirSync(testDir)
22+
.filter(function (file) {
23+
// Only keep the .js files
24+
return file.substr(-3) === ".js";
25+
})
26+
.forEach(function (file) {
27+
mocha.addFile(path.join(testDir, file));
28+
});
29+
30+
// Run the tests.
31+
mocha.run(function (failures) {
32+
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
33+
});
34+
```
35+
36+
`mocha.run()` returns a `Runner` instance which emits many [events](https://github.com/mochajs/mocha/blob/9a7053349589344236b20301de965030eaabfea9/lib/runner.js#L52) of interest.
37+
38+
Note that `run` (via `loadFiles`, which it calls) relies on Node's `require` to execute the test interface functions. Thus, files loaded by Mocha will be stored in Node's `require` cache and therefore tests in these files will not be re-run if `mocha.run()` is called again. If you want to run tests multiple times, you may need to clear Node's `require` cache before subsequent calls in whichever manner best suits your needs. The upcoming Mocha-6.0 release will provide `Mocha#unloadFiles`, which will remove all files loaded by `Mocha#loadFiles`.
39+
40+
Unfortunately, event listeners in multiple places are not yet configured for restartability; for now, we recommend recreating the `mocha` instance before rerunning to _ensure_ everything gets reset properly.
41+
42+
Find a fully [working example here](https://github.com/mochajs/mocha-examples/tree/main/packages/programmatic-usage).
43+
44+
## Set options
45+
46+
There are two ways to set the options to run the tests.
47+
48+
Firstly, you can set these options in the constructor object:
49+
50+
```javascript
51+
var mocha = new Mocha({
52+
ui: "tdd",
53+
reporter: "list",
54+
});
55+
```
56+
57+
Please check our [API documentation](https://mochajs.org/api/mocha) for a complete list of these options.
58+
59+
Secondly, on the `mocha` object, there are some chainable methods allowing you to change some more options.
60+
61+
Here is an example:
62+
63+
```javascript
64+
// Change the reporter to "list" before running the tests
65+
mocha.reporter("list").run();
66+
67+
// Change the UI to "tdd" before running the tests
68+
mocha.ui("tdd").run();
69+
70+
// Or do both changes before running the tests
71+
mocha.reporter("list").ui("tdd").run();
72+
```
73+
74+
Please check our [API documentation](https://mochajs.org/api/mocha) for more information.

docs/api-tutorials/jsdoc.tutorials.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
"custom-reporter": {
99
"title": "Create a Custom Reporter"
1010
},
11+
"developing-mocha": {
12+
"title": "Developing Mocha"
13+
},
14+
"programmatic-usage": {
15+
"title": "Programmatic usage"
16+
},
1117
"related-tools": {
1218
"title": "Related tools"
1319
},
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
There are a lot of reasons why you might want to automate running the tests using Mocha. Using the command-line can run into some problems if you want to load specific files, for example.
2+
3+
Here is an example of using Mocha programmatically:
4+
5+
```javascript
6+
var Mocha = require("mocha"),
7+
fs = require("fs"),
8+
path = require("path");
9+
10+
// Instantiate a Mocha instance.
11+
var mocha = new Mocha();
12+
13+
var testDir = "some/dir/test";
14+
15+
// Add each .js file to the mocha instance
16+
fs.readdirSync(testDir)
17+
.filter(function (file) {
18+
// Only keep the .js files
19+
return file.substr(-3) === ".js";
20+
})
21+
.forEach(function (file) {
22+
mocha.addFile(path.join(testDir, file));
23+
});
24+
25+
// Run the tests.
26+
mocha.run(function (failures) {
27+
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
28+
});
29+
```
30+
31+
`mocha.run()` returns a `Runner` instance which emits many [events](https://github.com/mochajs/mocha/blob/9a7053349589344236b20301de965030eaabfea9/lib/runner.js#L52) of interest.
32+
33+
Note that `run` (via `loadFiles`, which it calls) relies on Node's `require` to execute the test interface functions. Thus, files loaded by Mocha will be stored in Node's `require` cache and therefore tests in these files will not be re-run if `mocha.run()` is called again. If you want to run tests multiple times, you may need to clear Node's `require` cache before subsequent calls in whichever manner best suits your needs. The upcoming Mocha-6.0 release will provide `Mocha#unloadFiles`, which will remove all files loaded by `Mocha#loadFiles`.
34+
35+
Unfortunately, event listeners in multiple places are not yet configured for restartability; for now, we recommend recreating the `mocha` instance before rerunning to _ensure_ everything gets reset properly.
36+
37+
Find a fully [working example here](https://github.com/mochajs/mocha-examples/tree/main/packages/programmatic-usage).
38+
39+
## Set options
40+
41+
There are two ways to set the options to run the tests.
42+
43+
Firstly, you can set these options in the constructor object:
44+
45+
```javascript
46+
var mocha = new Mocha({
47+
ui: "tdd",
48+
reporter: "list",
49+
});
50+
```
51+
52+
Please check our [API documentation](https://mochajs.org/api/mocha) for a complete list of these options.
53+
54+
Secondly, on the `mocha` object, there are some chainable methods allowing you to change some more options.
55+
56+
Here is an example:
57+
58+
```javascript
59+
// Change the reporter to "list" before running the tests
60+
mocha.reporter("list").run();
61+
62+
// Change the UI to "tdd" before running the tests
63+
mocha.ui("tdd").run();
64+
65+
// Or do both changes before running the tests
66+
mocha.reporter("list").ui("tdd").run();
67+
```
68+
69+
Please check our [API documentation](https://mochajs.org/api/mocha) for more information.

0 commit comments

Comments
 (0)