Skip to content

Commit

Permalink
Add tests for --require ESM
Browse files Browse the repository at this point in the history
As both .mjs and type=module (combined with cjs for good measure).

Updated linter to allow tests to use spread operator (ecmaVersion 2018)

Allow --require'd module to be an object, or "module"
  • Loading branch information
JacobLey committed May 28, 2020
1 parent f28fd67 commit c9d1930
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .eslintrc.yml
Expand Up @@ -56,6 +56,8 @@ overrides:
browser: false
- files:
- test/**/*.{js,mjs}
parserOptions:
ecmaVersion: 2018
env:
mocha: true
globals:
Expand Down
5 changes: 4 additions & 1 deletion lib/cli/run-helpers.js
Expand Up @@ -92,7 +92,10 @@ exports.handleRequires = async (requires = []) => {
debug('resolved required file %s to %s', mod, modpath);
}
const requiredModule = await requireOrImport(modpath);
if (type(requiredModule) === 'object' && requiredModule.mochaHooks) {
if (
['object', 'module'].includes(type(requiredModule)) &&
requiredModule.mochaHooks
) {
const mochaHooksType = type(requiredModule.mochaHooks);
if (/function$/.test(mochaHooksType) || mochaHooksType === 'object') {
debug('found root hooks in required file %s', mod);
Expand Down
Empty file.
1 change: 1 addition & 0 deletions test/integration/fixtures/options/require/esm/package.json
@@ -0,0 +1 @@
{ "type": "module" }
@@ -0,0 +1,8 @@
export const mochaHooks = () => ({
beforeEach() {
console.log('esm beforeEach');
},
afterEach() {
console.log('esm afterEach');
},
});
@@ -0,0 +1,8 @@
export const mochaHooks = {
beforeAll() {
console.log('mjs beforeAll');
},
afterAll() {
console.log('mjs afterAll');
},
};
46 changes: 46 additions & 0 deletions test/integration/options/require.spec.js
@@ -1,6 +1,7 @@
'use strict';

var invokeMochaAsync = require('../helpers').invokeMochaAsync;
var utils = require('../../../lib/utils');

describe('--require', function() {
describe('when mocha run in serial mode', function() {
Expand Down Expand Up @@ -45,6 +46,51 @@ describe('--require', function() {
/beforeAll[\s\S]+?beforeAll array 1[\s\S]+?beforeAll array 2[\s\S]+?beforeEach[\s\S]+?beforeEach array 1[\s\S]+?beforeEach array 2[\s\S]+?afterEach[\s\S]+?afterEach array 1[\s\S]+?afterEach array 2[\s\S]+?afterAll[\s\S]+?afterAll array 1[\s\S]+?afterAll array 2/
);
});

describe.only('support ESM when style=module or .mjs extension', function() {
before(function() {
if (!utils.supportsEsModules()) this.skip();
});

it('should run root hooks when provided via mochaHooks', function() {
return expect(
invokeMochaAsync(
[
'--require=' +
require.resolve(
// as object
'../fixtures/options/require/root-hook-defs-esm.fixture.mjs'
),
'--require=' +
require.resolve(
// as function
'../fixtures/options/require/esm/root-hook-defs-esm.fixture.js'
),
'--require=' +
require.resolve(
// mixed with commonjs
'../fixtures/options/require/root-hook-defs-a.fixture.js'
),
require.resolve(
'../fixtures/options/require/root-hook-test.fixture.js'
)
],
{
env: {
...process.env,
NODE_OPTIONS:
+process.versions.node.split('.')[0] >= 13
? ''
: '--experimental-modules'
}
}
)[1],
'when fulfilled',
'to contain output',
/mjs beforeAll[\s\S]+?beforeAll[\s\S]+?esm beforeEach[\s\S]+?beforeEach[\s\S]+?esm afterEach[\s\S]+?afterEach[\s\S]+?mjs afterAll[\s\S]+?afterAll/
);
});
});
});

describe('when mocha in parallel mode', function() {
Expand Down

0 comments on commit c9d1930

Please sign in to comment.