Skip to content

Commit 8d6d1e3

Browse files
authored
fix(esm): allow import from mocha in parallel (#4574)
1 parent 37efe00 commit 8d6d1e3

File tree

7 files changed

+189
-15
lines changed

7 files changed

+189
-15
lines changed

lib/mocha.js

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,52 @@ exports.Suite = Suite;
9696
exports.Hook = require('./hook');
9797
exports.Test = require('./test');
9898

99+
let currentContext;
100+
exports.afterEach = function(...args) {
101+
(currentContext.afterEach || currentContext.teardown).apply(this, args);
102+
};
103+
exports.after = function(...args) {
104+
(currentContext.after || currentContext.suiteTeardown).apply(this, args);
105+
};
106+
exports.beforeEach = function(...args) {
107+
(currentContext.beforeEach || currentContext.setup).apply(this, args);
108+
};
109+
exports.before = function(...args) {
110+
(currentContext.before || currentContext.suiteSetup).apply(this, args);
111+
};
112+
exports.describe = function(...args) {
113+
(currentContext.describe || currentContext.suite).apply(this, args);
114+
};
115+
exports.describe.only = function(...args) {
116+
(currentContext.describe || currentContext.suite).only.apply(this, args);
117+
};
118+
exports.describe.skip = function(...args) {
119+
(currentContext.describe || currentContext.suite).skip.apply(this, args);
120+
};
121+
exports.it = function(...args) {
122+
(currentContext.it || currentContext.test).apply(this, args);
123+
};
124+
exports.it.only = function(...args) {
125+
(currentContext.it || currentContext.test).only.apply(this, args);
126+
};
127+
exports.it.skip = function(...args) {
128+
(
129+
currentContext.xit ||
130+
(currentContext.test && currentContext.test.skip)
131+
).apply(this, args);
132+
};
133+
exports.xdescribe = exports.describe.skip;
134+
exports.xit = exports.it.skip;
135+
exports.setup = exports.beforeEach;
136+
exports.suiteSetup = exports.before;
137+
exports.suiteTeardown = exports.after;
138+
exports.suite = exports.describe;
139+
exports.teardown = exports.afterEach;
140+
exports.test = exports.it;
141+
exports.run = function(...args) {
142+
currentContext.run.apply(this, args);
143+
};
144+
99145
/**
100146
* Constructs a new Mocha instance with `options`.
101147
*
@@ -351,20 +397,7 @@ Mocha.prototype.ui = function(ui) {
351397
bindInterface(this.suite);
352398

353399
this.suite.on(EVENT_FILE_PRE_REQUIRE, function(context) {
354-
exports.afterEach = context.afterEach || context.teardown;
355-
exports.after = context.after || context.suiteTeardown;
356-
exports.beforeEach = context.beforeEach || context.setup;
357-
exports.before = context.before || context.suiteSetup;
358-
exports.describe = context.describe || context.suite;
359-
exports.it = context.it || context.test;
360-
exports.xit = context.xit || (context.test && context.test.skip);
361-
exports.setup = context.setup || context.beforeEach;
362-
exports.suiteSetup = context.suiteSetup || context.before;
363-
exports.suiteTeardown = context.suiteTeardown || context.after;
364-
exports.suite = context.suite || context.describe;
365-
exports.teardown = context.teardown || context.afterEach;
366-
exports.test = context.test || context.it;
367-
exports.run = context.run;
400+
currentContext = context;
368401
});
369402

370403
return this;
@@ -1299,7 +1332,7 @@ Mocha.prototype.hasGlobalTeardownFixtures = function hasGlobalTeardownFixtures()
12991332
* A (sync) function to assert a user-supplied plugin implementation is valid.
13001333
*
13011334
* Defined in a {@link PluginDefinition}.
1302-
1335+
13031336
* @callback PluginValidator
13041337
* @param {*} value - Value to check
13051338
* @this {PluginDefinition}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const {runMochaAsync} = require('./helpers');
4+
5+
describe('common js require', () => {
6+
it('should be able to run a test where all mocha exports are used', async () => {
7+
const result = await runMochaAsync('common-js-require.fixture.js', [
8+
'--delay'
9+
]);
10+
expect(result.output, 'to contain', 'running before');
11+
expect(result.output, 'to contain', 'running suiteSetup');
12+
expect(result.output, 'to contain', 'running setup');
13+
expect(result.output, 'to contain', 'running beforeEach');
14+
expect(result.output, 'to contain', 'running it');
15+
expect(result.output, 'to contain', 'running afterEach');
16+
expect(result.output, 'to contain', 'running teardown');
17+
expect(result.output, 'to contain', 'running suiteTeardown');
18+
expect(result.output, 'to contain', 'running after');
19+
});
20+
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const { afterEach,
2+
after,
3+
beforeEach,
4+
before,
5+
describe,
6+
xdescribe,
7+
it,
8+
xit,
9+
setup,
10+
suiteSetup,
11+
suiteTeardown,
12+
suite,
13+
teardown,
14+
test,
15+
run } = require('../../..');
16+
17+
18+
suite('root suite', () => {
19+
setup(() => {
20+
console.log('running setup');
21+
})
22+
before(() => {
23+
console.log('running before');
24+
});
25+
beforeEach(() => {
26+
console.log('running beforeEach');
27+
});
28+
afterEach(() => {
29+
console.log('running afterEach');
30+
});
31+
after(() => {
32+
console.log('running after');
33+
});
34+
teardown(() => {
35+
console.log('running teardown');
36+
});
37+
suiteSetup(() => {
38+
console.log('running suiteSetup');
39+
});
40+
suiteTeardown(() => {
41+
console.log('running suiteTeardown');
42+
});
43+
44+
describe.only('describe only', () => {
45+
it('it', () => {
46+
console.log('running it');
47+
});
48+
xit('it', () => {
49+
console.log('running xit');
50+
});
51+
it.only('it.only', () => {
52+
console.log('running it.only');
53+
});
54+
it.skip('it.skip', () => {
55+
console.log('running it.skip');
56+
});
57+
test('test', () => {
58+
console.log('running test');
59+
});
60+
});
61+
62+
describe('describe', () => {});
63+
64+
xdescribe('xdescribe', () => {});
65+
66+
describe.skip('describe.skip', () => {});
67+
68+
suite.only('suite only', () => {});
69+
70+
suite.skip('suite.skip', () => {});
71+
72+
});
73+
74+
// using `run` here makes it so this suite needs to be run with `--delay` mode.
75+
// adding it here to test that `run` is correctly exported from mocha.
76+
setTimeout(run, 0);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {describe,it} from "../../../../index.js";
2+
3+
describe('test1', () => {
4+
it('should pass', () => {});
5+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {describe,it} from "../../../../index.js";
2+
3+
describe('test2', () => {
4+
it('should pass', () => {});
5+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {describe,it} from "../../../../index.js";
2+
3+
describe('test3', () => {
4+
it('should fail', () => {
5+
throw new Error('expecting this error to fail');
6+
});
7+
});

test/integration/parallel.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const {runMochaJSONAsync} = require('./helpers');
5+
const semver = require('semver');
6+
7+
describe('parallel run', () => {
8+
/**
9+
* @see https://github.com/mochajs/mocha/issues/4559
10+
*/
11+
it('should allow `import {it} from "mocha"` module syntax', async () => {
12+
if (semver.major(process.version) <= 10) {
13+
console.log(
14+
`[SKIPPED] for node ${process.version} (es module syntax isn't supported on node <= 10)`
15+
);
16+
} else {
17+
const result = await runMochaJSONAsync('parallel/test3.mjs', [
18+
'--parallel',
19+
'--jobs',
20+
'2',
21+
require.resolve('./fixtures/parallel/test1.mjs'),
22+
require.resolve('./fixtures/parallel/test2.mjs')
23+
]);
24+
assert.strictEqual(result.stats.failures, 1);
25+
assert.strictEqual(result.stats.passes, 2);
26+
}
27+
});
28+
});

0 commit comments

Comments
 (0)