Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(esm): allow import from mocha in parallel #4574

Merged
merged 4 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 49 additions & 15 deletions lib/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,53 @@ exports.Suite = Suite;
exports.Hook = require('./hook');
exports.Test = require('./test');

let currentContext;
exports.afterEach = function(...args) {
(currentContext.afterEach || currentContext.teardown).apply(this, args);
};
exports.after = function(...args) {
(currentContext.after || currentContext.suiteTeardown).apply(this, args);
};
exports.beforeEach = function(...args) {
(currentContext.beforeEach || currentContext.setup).apply(this, args);
};
exports.before = function(...args) {
(currentContext.before || currentContext.suiteSetup).apply(this, args);
};
exports.describe = function(...args) {
nicojs marked this conversation as resolved.
Show resolved Hide resolved
(currentContext.describe || currentContext.suite).apply(this, args);
};
exports.it = function(...args) {
(currentContext.it || currentContext.test).apply(this, args);
};
exports.xit = function(...args) {
(
currentContext.xit ||
(currentContext.test && currentContext.test.skip)
).apply(this, args);
};
exports.setup = function(...args) {
(currentContext.setup || currentContext.beforeEach).apply(this, args);
};
exports.suiteSetup = function(...args) {
(currentContext.suiteSetup || currentContext.before).apply(this, args);
};
exports.suiteTeardown = function(...args) {
(currentContext.suiteTeardown || currentContext.after).apply(this, args);
};
exports.suite = function(...args) {
(currentContext.suite || currentContext.describe).apply(this, args);
};
exports.teardown = function(...args) {
(currentContext.teardown || currentContext.afterEach).apply(this, args);
};
exports.test = function(...args) {
(currentContext.test || currentContext.it).apply(this, args);
};
exports.run = function(...args) {
currentContext.run.apply(this, args);
};

/**
* Constructs a new Mocha instance with `options`.
*
Expand Down Expand Up @@ -351,20 +398,7 @@ Mocha.prototype.ui = function(ui) {
bindInterface(this.suite);

this.suite.on(EVENT_FILE_PRE_REQUIRE, function(context) {
exports.afterEach = context.afterEach || context.teardown;
exports.after = context.after || context.suiteTeardown;
exports.beforeEach = context.beforeEach || context.setup;
exports.before = context.before || context.suiteSetup;
exports.describe = context.describe || context.suite;
exports.it = context.it || context.test;
exports.xit = context.xit || (context.test && context.test.skip);
exports.setup = context.setup || context.beforeEach;
exports.suiteSetup = context.suiteSetup || context.before;
exports.suiteTeardown = context.suiteTeardown || context.after;
exports.suite = context.suite || context.describe;
exports.teardown = context.teardown || context.afterEach;
exports.test = context.test || context.it;
exports.run = context.run;
currentContext = context;
});

return this;
Expand Down Expand Up @@ -1299,7 +1333,7 @@ Mocha.prototype.hasGlobalTeardownFixtures = function hasGlobalTeardownFixtures()
* A (sync) function to assert a user-supplied plugin implementation is valid.
*
* Defined in a {@link PluginDefinition}.

* @callback PluginValidator
* @param {*} value - Value to check
* @this {PluginDefinition}
Expand Down
5 changes: 5 additions & 0 deletions test/integration/fixtures/parallel/test1.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {describe,it} from "../../../../index.js";

describe('test1', () => {
it('should pass', () => {});
});
5 changes: 5 additions & 0 deletions test/integration/fixtures/parallel/test2.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {describe,it} from "../../../../index.js";

describe('test2', () => {
it('should pass', () => {});
});
7 changes: 7 additions & 0 deletions test/integration/fixtures/parallel/test3.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {describe,it} from "../../../../index.js";

describe('test3', () => {
it('should fail', () => {
throw new Error('expecting this error to fail');
Copy link

@joielee09 joielee09 Mar 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicojs hello! I was reviewing your PR, and have a question for this change.
You made three different tests, test1, test2, test3. Can you explain about this tests?
Why test1 and test2 pass whereas test3 fail? Also Why do we need test1 and test2 each, when they have the same dir path and they both pass. Thank you 👩

});
});
28 changes: 28 additions & 0 deletions test/integration/parallel.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const assert = require('assert');
const {runMochaJSONAsync} = require('./helpers');
const semver = require('semver');

describe('parallel run', () => {
/**
* @see https://github.com/mochajs/mocha/issues/4559
*/
it('should allow `import {it} from "mocha"` module syntax', async () => {
if (semver.major(process.version) <= 10) {
console.log(
`[SKIPPED] for node ${process.version} (es module syntax isn't supported on node <= 10)`
);
} else {
const result = await runMochaJSONAsync('parallel/test3.mjs', [
'--parallel',
'--jobs',
'2',
require.resolve('./fixtures/parallel/test1.mjs'),
require.resolve('./fixtures/parallel/test2.mjs')
]);
assert.strictEqual(result.stats.failures, 1);
assert.strictEqual(result.stats.passes, 2);
}
});
});