Skip to content

Commit 89f7c0d

Browse files
committed
docs: migrate developer-focused wiki pages to main documentation
Migrate 2 developer-focused wiki pages to both docs systems: - Using Mocha programmatically - Developing mocha Part of #5248 wiki migration effort. Signed-off-by: Kelechi Ebiri <[email protected]>
1 parent 6654704 commit 89f7c0d

File tree

6 files changed

+229
-0
lines changed

6 files changed

+229
-0
lines changed

docs-next/astro.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ export default defineConfig({
112112
label: "Counting assertions",
113113
slug: "explainers/count-assertions",
114114
},
115+
{
116+
label: "Developing Mocha",
117+
slug: "explainers/developing-mocha",
118+
},
115119
{
116120
label: "Find global leaks",
117121
slug: "explainers/find-global-leaks",
@@ -120,6 +124,10 @@ export default defineConfig({
120124
label: "Node.js native ESM support",
121125
slug: "explainers/nodejs-native-esm-support",
122126
},
127+
{
128+
label: "Programmatic usage",
129+
slug: "explainers/programmatic-usage",
130+
},
123131
{
124132
label: "Related tools",
125133
slug: "explainers/related-tools",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
description: Developing Mocha".
3+
title: Developing Mocha
4+
---
5+
6+
This page contains info on developing Mocha itself.
7+
8+
## Environment setup
9+
10+
When you contribute to mocha itself, 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 can be useful.
11+
12+
For example, [WebSocket.io](https://github.com/LearnBoost/websocket.io/):
13+
14+
$ git clone https://github.com/LearnBoost/websocket.io.git
15+
16+
Retreive websocket.io's dependencies, which will include the stable version of mocha:
17+
18+
$ cd websocket.io/
19+
$ npm install
20+
21+
Replace the mocha dependency by the current git repository:
22+
23+
$ cd node_modules/
24+
$ mv mocha/ mocha.save
25+
$ git clone https://github.com/visionmedia/mocha.git
26+
27+
Install mocha's dependencies for the development version:
28+
29+
$ cd mocha
30+
$ npm install
31+
32+
Run websocket.io's test suite using the development version you just installed:
33+
34+
$ cd ../..
35+
$ ./node_modules/.bin/mocha
36+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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).filter(function(file) {
22+
// Only keep the .js files
23+
return file.substr(-3) === '.js';
24+
25+
}).forEach(function(file) {
26+
mocha.addFile(
27+
path.join(testDir, file)
28+
);
29+
});
30+
31+
// Run the tests.
32+
mocha.run(function(failures) {
33+
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
34+
});
35+
```
36+
37+
`mocha.run()` returns a `Runner` instance which emits many [events](https://github.com/mochajs/mocha/blob/8cae7a34f0b6eafeb16567beb8852b827cc5956b/lib/runner.js#L47-L57) of interest.
38+
39+
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`.
40+
41+
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.
42+
43+
Find a fully [working example here](https://github.com/mochajs/mocha-examples/tree/master/packages/programmatic-usage).
44+
45+
## Set options
46+
47+
There are two ways to set the options to run the tests.
48+
49+
Firstly, you can set these options in the constructor object:
50+
51+
```javascript
52+
var mocha = new Mocha({
53+
ui: 'tdd',
54+
reporter: 'list'
55+
});
56+
```
57+
58+
Please check our [API documentation](https://mochajs.org/api/mocha) for a complete list of these options.
59+
60+
Secondly, on the `mocha` object, there are some chainable methods allowing you to change some more options.
61+
62+
Here is an example:
63+
64+
```javascript
65+
// Change the reporter to "list" before running the tests
66+
mocha.reporter('list').run();
67+
68+
// Change the UI to "tdd" before running the tests
69+
mocha.ui('tdd').run();
70+
71+
// Or do both changes before running the tests
72+
mocha.reporter('list').ui('tdd').run();
73+
```
74+
75+
Please check our [API documentation](https://mochajs.org/api/mocha) for more information.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Developing Mocha
2+
3+
This page contains info on developing Mocha itself.
4+
5+
## Environment setup
6+
7+
When you contribute to mocha itself, 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 can be useful.
8+
9+
For example, [WebSocket.io](https://github.com/LearnBoost/websocket.io/):
10+
11+
$ git clone https://github.com/LearnBoost/websocket.io.git
12+
13+
Retreive websocket.io's dependencies, which will include the stable version of mocha:
14+
15+
$ cd websocket.io/
16+
$ npm install
17+
18+
Replace the mocha dependency by the current git repository:
19+
20+
$ cd node_modules/
21+
$ mv mocha/ mocha.save
22+
$ git clone https://github.com/visionmedia/mocha.git
23+
24+
Install mocha's dependencies for the development version:
25+
26+
$ cd mocha
27+
$ npm install
28+
29+
Run websocket.io's test suite using the development version you just installed:
30+
31+
$ cd ../..
32+
$ ./node_modules/.bin/mocha

docs/api-tutorials/jsdoc.tutorials.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
"custom-reporter": {
66
"title": "Create a Custom Reporter"
77
},
8+
"developing-mocha": {
9+
"title": "Developing Mocha"
10+
},
11+
"programmatic-usage": {
12+
"title": "Programmatic usage"
13+
},
814
"related-tools": {
915
"title": "Related tools"
1016
},
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# using Mocha programmatically
2+
3+
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.
4+
5+
Here is an example of using Mocha programmatically:
6+
7+
```javascript
8+
var Mocha = require('mocha'),
9+
fs = require('fs'),
10+
path = require('path');
11+
12+
// Instantiate a Mocha instance.
13+
var mocha = new Mocha();
14+
15+
var testDir = 'some/dir/test'
16+
17+
// Add each .js file to the mocha instance
18+
fs.readdirSync(testDir).filter(function(file) {
19+
// Only keep the .js files
20+
return file.substr(-3) === '.js';
21+
22+
}).forEach(function(file) {
23+
mocha.addFile(
24+
path.join(testDir, file)
25+
);
26+
});
27+
28+
// Run the tests.
29+
mocha.run(function(failures) {
30+
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
31+
});
32+
```
33+
34+
`mocha.run()` returns a `Runner` instance which emits many [events](https://github.com/mochajs/mocha/blob/8cae7a34f0b6eafeb16567beb8852b827cc5956b/lib/runner.js#L47-L57) of interest.
35+
36+
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`.
37+
38+
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.
39+
40+
Find a fully [working example here](https://github.com/mochajs/mocha-examples/tree/master/packages/programmatic-usage).
41+
42+
## Set options
43+
44+
There are two ways to set the options to run the tests.
45+
46+
Firstly, you can set these options in the constructor object:
47+
48+
```javascript
49+
var mocha = new Mocha({
50+
ui: 'tdd',
51+
reporter: 'list'
52+
});
53+
```
54+
55+
Please check our [API documentation](https://mochajs.org/api/mocha) for a complete list of these options.
56+
57+
Secondly, on the `mocha` object, there are some chainable methods allowing you to change some more options.
58+
59+
Here is an example:
60+
61+
```javascript
62+
// Change the reporter to "list" before running the tests
63+
mocha.reporter('list').run();
64+
65+
// Change the UI to "tdd" before running the tests
66+
mocha.ui('tdd').run();
67+
68+
// Or do both changes before running the tests
69+
mocha.reporter('list').ui('tdd').run();
70+
```
71+
72+
Please check our [API documentation](https://mochajs.org/api/mocha) for more information.

0 commit comments

Comments
 (0)