From e9834fdc898678ffb5b8757c9cc5cdd2a4e0a8de Mon Sep 17 00:00:00 2001 From: Jacob Ley Date: Thu, 28 May 2020 16:21:09 -0400 Subject: [PATCH] Add tests for --require ESM 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" --- .eslintrc.yml | 2 + lib/cli/run-helpers.js | 5 +- test/integration/fixtures/esm/require.mjs | 0 .../fixtures/options/require/esm/package.json | 1 + .../require/esm/root-hook-defs-esm.fixture.js | 8 ++++ .../require/root-hook-defs-esm.fixture.mjs | 8 ++++ test/integration/options/require.spec.js | 46 +++++++++++++++++++ 7 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 test/integration/fixtures/esm/require.mjs create mode 100644 test/integration/fixtures/options/require/esm/package.json create mode 100644 test/integration/fixtures/options/require/esm/root-hook-defs-esm.fixture.js create mode 100644 test/integration/fixtures/options/require/root-hook-defs-esm.fixture.mjs diff --git a/.eslintrc.yml b/.eslintrc.yml index 2a3fa281df..a10eb0ae21 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -56,6 +56,8 @@ overrides: browser: false - files: - test/**/*.{js,mjs} + parserOptions: + ecmaVersion: 2018 env: mocha: true globals: diff --git a/lib/cli/run-helpers.js b/lib/cli/run-helpers.js index 513e930e2a..10cb06ab07 100644 --- a/lib/cli/run-helpers.js +++ b/lib/cli/run-helpers.js @@ -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); diff --git a/test/integration/fixtures/esm/require.mjs b/test/integration/fixtures/esm/require.mjs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/integration/fixtures/options/require/esm/package.json b/test/integration/fixtures/options/require/esm/package.json new file mode 100644 index 0000000000..5ffd9800b9 --- /dev/null +++ b/test/integration/fixtures/options/require/esm/package.json @@ -0,0 +1 @@ +{ "type": "module" } diff --git a/test/integration/fixtures/options/require/esm/root-hook-defs-esm.fixture.js b/test/integration/fixtures/options/require/esm/root-hook-defs-esm.fixture.js new file mode 100644 index 0000000000..3e0a2f775c --- /dev/null +++ b/test/integration/fixtures/options/require/esm/root-hook-defs-esm.fixture.js @@ -0,0 +1,8 @@ +export const mochaHooks = () => ({ + beforeEach() { + console.log('esm beforeEach'); + }, + afterEach() { + console.log('esm afterEach'); + }, +}); diff --git a/test/integration/fixtures/options/require/root-hook-defs-esm.fixture.mjs b/test/integration/fixtures/options/require/root-hook-defs-esm.fixture.mjs new file mode 100644 index 0000000000..6597d65be0 --- /dev/null +++ b/test/integration/fixtures/options/require/root-hook-defs-esm.fixture.mjs @@ -0,0 +1,8 @@ +export const mochaHooks = { + beforeAll() { + console.log('mjs beforeAll'); + }, + afterAll() { + console.log('mjs afterAll'); + }, +}; diff --git a/test/integration/options/require.spec.js b/test/integration/options/require.spec.js index ca50af8607..e24b605f1d 100644 --- a/test/integration/options/require.spec.js +++ b/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() { @@ -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('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() {