Skip to content

Commit 80c9387

Browse files
authored
Merge branch 'main' into feat/esm-config
2 parents 0823a91 + f75d150 commit 80c9387

32 files changed

+664
-99
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

.github/workflows/npm-script.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
- uses: actions/checkout@v5
6363
with:
6464
persist-credentials: false
65-
- uses: actions/setup-node@v5
65+
- uses: actions/setup-node@v6
6666
with:
6767
node-version: ${{ matrix.node_version }}
6868
cache: "npm"

.github/workflows/release-please.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
- uses: actions/checkout@v5
3737
with:
3838
show-progress: false
39-
- uses: actions/setup-node@v5
39+
- uses: actions/setup-node@v6
4040
with:
4141
# The 22.11.0 is instead of lts per https://github.com/mochajs/mocha/issues/5278
4242
node-version: "22.11.0"

MAINTAINERS.md

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ Your public behavior, whether in the physical or virtual world, reflects upon th
9090
> If you don't understand the code of conduct, or why it exists, it is _your responsibility_ to educate yourself.
9191
> This does not imply the CoC is immutable.
9292
93-
Furthermore, a maintainer is a contributor who **contributes regularly**, or expresses a _desire to do so._ That could be every day--but it might be once a week, or even once a month.
94-
Your boss doesn't work here; contribute as often as you wish.
95-
We are all people with Real Lives, and for many of us, contributing to OSS is just a hobby!
93+
Furthermore, a maintainer is a contributor who **contributes regularly**. That can be daily, weekly, or even just a few times a month -- as long as it's regular.
94+
We are all people with Real Lives, and for many of us, contributing to OSS is just an occasional hobby!
9695

9796
Finally, a maintainer must help define what makes Mocha "Mocha".
9897
At minimum, a maintainer must _understand_ the current definition (if a maintainer is not interested in decision-making).
@@ -108,7 +107,7 @@ As maintainers, _we work together_ to learn about the nature of these questions.
108107

109108
A maintainer _must_ also have 2FA enabled on their GitHub account.
110109

111-
> If you think that you aren't familiar with mocha's internals enough to contribute, please watch [This walkthrough video!](https://youtu.be/zLayCLcIno0)
110+
> If you think that you aren't familiar with mocha's internals enough to contribute, please watch [this walkthrough video](https://youtu.be/zLayCLcIno0)!
112111
113112
#### The Rights of a Maintainer
114113

@@ -127,6 +126,28 @@ You may choose to do zero or more of these _at their discretion_:
127126
> For example, a spelling correction in `CHANGELOG.md` may not require a pull request.
128127
> A change to a reporter's output most certainly would! Maintainers are trusted to use their best judgement; if unsure, err on the side of caution.
129128
129+
#### Expected Activity Levels
130+
131+
We generally expect maintainers to average at least:
132+
133+
- Multiple contributions of any kind in a week
134+
- One meaningful, non-trivial contribution per month
135+
136+
These are rough averages, not strict minimums!
137+
It's totally fine to skip a week or two here and there, as long as you do a little more before or after.
138+
Meaningful, non-trivial contributions can be a well-thought-out comment, sending a non-straightforward pull request, or anything else that adds real value to the project.
139+
140+
If you have to step away longer than a couple of weeks, that's totally fine too.
141+
Just let the other maintainers know ahead of time.
142+
143+
- If you need to step away for up to a month or two, don't sweat it.
144+
- If we don't hear from you for a month without prior notice, we'll check in with you to see if you want to remain a maintainer.
145+
- After three months of inactivity, or two without prior notice, we'll likely remove you as a maintainer.
146+
147+
Again, don't stress about this if you can't always be available.
148+
Your boss doesn't work here; contribute as often as you reasonably can.
149+
Even if you step down or are removed, if you can come back and be regularly active we'll happily reinstate you.
150+
130151
#### About "Owners"
131152

132153
Some maintainers will have full admin rights to the [mochajs org](https://github.com/mochajs) and/or will have access to publish to npm.
@@ -142,6 +163,7 @@ If that fails, we decide by a simple vote.
142163

143164
Active maintainers will make an effort to solicit feedback from others before making important or potentially controversial decisions.
144165
Given the varying geographical distribution and availability of the maintenance team, we resolve to do the best we can to solicit feedback.
166+
We will wait at least two weeks for consensus votes in most cases, and a month for especially important decisions.
145167

146168
In other words, to have your opinion heard, participate regularly.
147169
The rest of the team won't wait on feedback that isn't necessarily forthcoming!

docs-next/astro.config.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ export default defineConfig({
104104
{
105105
collapsed: true,
106106
items: [
107-
{
108-
label: "Detecting multiple calls to done()",
109-
slug: "explainers/detecting-multiple-calls-to-done",
110-
},
111107
{
112108
label: "Compilers deprecation",
113109
slug: "explainers/compilers-deprecation",
@@ -116,14 +112,30 @@ export default defineConfig({
116112
label: "Counting assertions",
117113
slug: "explainers/count-assertions",
118114
},
115+
{
116+
label: "Detecting multiple calls to done()",
117+
slug: "explainers/detecting-multiple-calls-to-done",
118+
},
119+
{
120+
label: "Environment variables",
121+
slug: "explainers/environment-variables",
122+
},
119123
{
120124
label: "Find global leaks",
121125
slug: "explainers/find-global-leaks",
122126
},
127+
{
128+
label: "Global variables",
129+
slug: "explainers/global-variables",
130+
},
123131
{
124132
label: "Node.js native ESM support",
125133
slug: "explainers/nodejs-native-esm-support",
126134
},
135+
{
136+
label: "Programmatic usage",
137+
slug: "explainers/programmatic-usage",
138+
},
127139
{
128140
label: "Related tools",
129141
slug: "explainers/related-tools",
@@ -140,6 +152,10 @@ export default defineConfig({
140152
label: "Spies",
141153
slug: "explainers/spies",
142154
},
155+
{
156+
label: "Stub stdout",
157+
slug: "explainers/stub-stdout",
158+
},
143159
{
144160
label: "Tagging with --grep",
145161
slug: "explainers/tagging",

docs-next/src/content/docs/explainers/count-assertions.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: How to count assertions.
3-
title: Count assertions
3+
title: Counting assertions
44
---
55

66
While Mocha itself does not provide an assertion layer and cannot provide assertion counting, it's relatively easy to integrate this behavior using hooks. The following is a simplified version of an assertion counter:
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
description: Learn how to use environment variables in Mocha tests
3+
title: "Environment variables"
4+
---
5+
6+
Sometimes you might want your test to make use of variables set on an environment level. Common reasons for this include:
7+
8+
- Flagging a run as CI
9+
- Setting the domain of an application
10+
- Pointing to a test database
11+
- Setting a key which you want to remain secure
12+
13+
The best way to do this is to use NodeJS's native environment variables.
14+
15+
## Example
16+
17+
In your `package.json` or directly in a terminal set your variable name and value.
18+
19+
```bash
20+
env CI=STAGE mocha
21+
```
22+
23+
Then in your test or code reference it via `process.env.CI`. e.g.
24+
25+
```javascript
26+
if (process.env.CI === "STAGE") // do something
27+
```
28+
29+
The value is available from anywhere in a spec file allowing you to set values from it before the tests run. e.g.
30+
31+
```javascript
32+
import { equal } from "assert";
33+
import { loadPage } from "your-code";
34+
const URL = `${process.env.APP_HOST}/${process.env.APP_URI}`;
35+
describe('Test the page loads', () {
36+
it('goes to URL', () => {
37+
const page = loadPage(URL);
38+
equal(page.url, URL);
39+
});
40+
});
41+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
description: How to work with global variables in Mocha tests
3+
title: "Global variables"
4+
---
5+
6+
Related issue [#1582](https://github.com/mochajs/mocha/issues/1582)
7+
8+
If your test manipulates global variables, a reasonable expectation is that you will clean up after yourself. This includes commonly called methods such as `process.stdout.write()` or `console.log()`.
9+
10+
```js
11+
var expect = require("chai").expect;
12+
13+
describe("my nice test", function () {
14+
var write,
15+
log,
16+
output = "";
17+
18+
// restore process.stdout.write() and console.log() to their previous glory
19+
var cleanup = function () {
20+
process.stdout.write = write;
21+
console.log = log;
22+
output = "";
23+
};
24+
25+
beforeEach(function () {
26+
// store these functions to restore later because we are messing with them
27+
write = process.stdout.write;
28+
log = console.log;
29+
30+
// our stub will concatenate any output to a string
31+
process.stdout.write = console.log = function (s) {
32+
output += s;
33+
};
34+
});
35+
36+
// restore after each test
37+
afterEach(cleanup);
38+
39+
it("should suppress all output if a non-AssertionError was thrown", function () {
40+
process.stdout.write("foo");
41+
console.log("bar");
42+
// uncomment below line to suppress output, which is bad
43+
// expect(output).to.equal(foobar);
44+
expect(output).to.equal("foobar");
45+
});
46+
47+
it("should not suppress any output", function () {
48+
try {
49+
process.stdout.write("foo");
50+
console.log("bar");
51+
// uncomment below line to just throw an AssertionError
52+
// expect(output).to.equal('barfoo');
53+
expect(output).to.equal(foobar); // ReferenceError
54+
} catch (e) {
55+
cleanup();
56+
throw e;
57+
}
58+
});
59+
});
60+
```
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.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
description: Learn how to properly stub stdout for testing console output
3+
title: "Stubbing stdout"
4+
---
5+
6+
If you want to stub `stdout` inside your own code (via `process.stdout.write` or `console.log`) there is a potential to hinder Mocha's reporter output. This is because they rely on the same mechanisms to print results of the tests.
7+
8+
i.e.
9+
10+
```javascript
11+
it('should do, but it do not', function() {
12+
// user wants to hide output generated by console.log calls in fn 'foo'
13+
console.log = function() {};
14+
foo();
15+
16+
expect(...)
17+
});
18+
```
19+
20+
This will result in a faulty reporter output.
21+
22+
The correct way to handle this is to stub before and restore after the function call.
23+
24+
```javascript
25+
it('will do', function() {
26+
var consoleLogStub = sinon.stub(console, 'log');
27+
foo();
28+
consoleLogStub.restore();
29+
30+
expect(...)
31+
});
32+
```
33+
34+
Now the reporters can run and print the assertion without any interference, while stubbing `stdout` inside your function.

0 commit comments

Comments
 (0)