You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: .github/DEVELOPMENT.md
+29Lines changed: 29 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,3 +16,32 @@ If you are having trouble, don't be afraid to [ask for help](./CONTRIBUTING.md#
16
16
- Do not use `yarn install` or `pnpm install`.
17
17
- Some optional dependencies may fail; you can safely ignore these unless you are trying to build the documentation.
18
18
- 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/):
Copy file name to clipboardExpand all lines: MAINTAINERS.md
+26-4Lines changed: 26 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,9 +90,8 @@ Your public behavior, whether in the physical or virtual world, reflects upon th
90
90
> If you don't understand the code of conduct, or why it exists, it is _your responsibility_ to educate yourself.
91
91
> This does not imply the CoC is immutable.
92
92
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!
96
95
97
96
Finally, a maintainer must help define what makes Mocha "Mocha".
98
97
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.
108
107
109
108
A maintainer _must_ also have 2FA enabled on their GitHub account.
110
109
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)!
112
111
113
112
#### The Rights of a Maintainer
114
113
@@ -127,6 +126,28 @@ You may choose to do zero or more of these _at their discretion_:
127
126
> For example, a spelling correction in `CHANGELOG.md` may not require a pull request.
128
127
> 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.
129
128
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
+
130
151
#### About "Owners"
131
152
132
153
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.
142
163
143
164
Active maintainers will make an effort to solicit feedback from others before making important or potentially controversial decisions.
144
165
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.
145
167
146
168
In other words, to have your opinion heard, participate regularly.
147
169
The rest of the team won't wait on feedback that isn't necessarily forthcoming!
Copy file name to clipboardExpand all lines: docs-next/src/content/docs/explainers/count-assertions.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
description: How to count assertions.
3
-
title: Count assertions
3
+
title: Counting assertions
4
4
---
5
5
6
6
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:
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
+
varcleanup=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
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 =newMocha();
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
+
returnfile.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 =newMocha({
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.
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