diff --git a/lib/cli/handle-requires.js b/lib/cli/handle-requires.js new file mode 100644 index 0000000000..e2e447e098 --- /dev/null +++ b/lib/cli/handle-requires.js @@ -0,0 +1,38 @@ +'use strict'; +const fs = require('fs'); +const path = require('path'); +const debug = require('debug')('mocha:cli:handle:requires'); +const {requireOrImport} = require('../nodejs/esm-utils'); +const PluginLoader = require('../plugin-loader'); + +/** + * `require()` the modules as required by `--require `. + * + * Returns array of `mochaHooks` exports, if any. + * @param {string[]} requires - Modules to require + * @returns {Promise} Plugin implementations + * @private + */ +exports.handleRequires = async (requires = [], {ignoredPlugins = []} = {}) => { + const pluginLoader = PluginLoader.create({ignore: ignoredPlugins}); + for await (const mod of requires) { + let modpath = mod; + // this is relative to cwd + if (fs.existsSync(mod) || fs.existsSync(`${mod}.js`)) { + modpath = path.resolve(mod); + debug('resolved required file %s to %s', mod, modpath); + } + const requiredModule = await requireOrImport(modpath); + if (requiredModule && typeof requiredModule === 'object') { + if (pluginLoader.load(requiredModule)) { + debug('found one or more plugin implementations in %s', modpath); + } + } + debug('loaded required module "%s"', mod); + } + const plugins = await pluginLoader.finalize(); + if (Object.keys(plugins).length) { + debug('finalized plugin implementations: %O', plugins); + } + return plugins; +}; diff --git a/lib/cli/run-helpers.js b/lib/cli/run-helpers.js index 078ca7e434..288ddad70c 100644 --- a/lib/cli/run-helpers.js +++ b/lib/cli/run-helpers.js @@ -7,15 +7,12 @@ * @private */ -const fs = require('fs'); const path = require('path'); const debug = require('debug')('mocha:cli:run:helpers'); const {watchRun, watchParallelRun} = require('./watch-run'); const collectFiles = require('./collect-files'); const {format} = require('util'); const {createInvalidLegacyPluginError} = require('../errors'); -const {requireOrImport} = require('../nodejs/esm-utils'); -const PluginLoader = require('../plugin-loader'); /** * Exits Mocha when tests + code under test has finished execution (default) @@ -74,38 +71,6 @@ const exitMocha = code => { exports.list = str => Array.isArray(str) ? exports.list(str.join(',')) : str.split(/ *, */); -/** - * `require()` the modules as required by `--require `. - * - * Returns array of `mochaHooks` exports, if any. - * @param {string[]} requires - Modules to require - * @returns {Promise} Plugin implementations - * @private - */ -exports.handleRequires = async (requires = [], {ignoredPlugins = []} = {}) => { - const pluginLoader = PluginLoader.create({ignore: ignoredPlugins}); - for await (const mod of requires) { - let modpath = mod; - // this is relative to cwd - if (fs.existsSync(mod) || fs.existsSync(`${mod}.js`)) { - modpath = path.resolve(mod); - debug('resolved required file %s to %s', mod, modpath); - } - const requiredModule = await requireOrImport(modpath); - if (requiredModule && typeof requiredModule === 'object') { - if (pluginLoader.load(requiredModule)) { - debug('found one or more plugin implementations in %s', modpath); - } - } - debug('loaded required module "%s"', mod); - } - const plugins = await pluginLoader.finalize(); - if (Object.keys(plugins).length) { - debug('finalized plugin implementations: %O', plugins); - } - return plugins; -}; - /** * Collect and load test files, then run mocha instance. * @param {Mocha} mocha - Mocha instance diff --git a/lib/cli/run.js b/lib/cli/run.js index 66c8cbbb66..988264466a 100644 --- a/lib/cli/run.js +++ b/lib/cli/run.js @@ -16,12 +16,8 @@ const { createMissingArgumentError } = require('../errors'); -const { - list, - handleRequires, - validateLegacyPlugin, - runMocha -} = require('./run-helpers'); +const {list, validateLegacyPlugin, runMocha} = require('./run-helpers'); +const {handleRequires} = require('./handle-requires'); const {ONE_AND_DONES, ONE_AND_DONE_ARGS} = require('./one-and-dones'); const debug = require('debug')('mocha:cli:run'); const defaults = require('../mocharc'); diff --git a/lib/nodejs/worker.js b/lib/nodejs/worker.js index cf5655e80d..f6f5095b70 100644 --- a/lib/nodejs/worker.js +++ b/lib/nodejs/worker.js @@ -12,7 +12,8 @@ const { } = require('../errors'); const workerpool = require('workerpool'); const Mocha = require('../mocha'); -const {handleRequires, validateLegacyPlugin} = require('../cli/run-helpers'); +const {validateLegacyPlugin} = require('../cli/run-helpers'); +const {handleRequires} = require('../cli/handle-requires'); const d = require('debug'); const debug = d.debug(`mocha:parallel:worker:${process.pid}`); const isDebugEnabled = d.enabled(`mocha:parallel:worker:${process.pid}`); diff --git a/test/node-unit/worker.spec.js b/test/node-unit/worker.spec.js index 8a7d73dca7..8fd09126e4 100644 --- a/test/node-unit/worker.spec.js +++ b/test/node-unit/worker.spec.js @@ -54,10 +54,13 @@ describe('worker', function () { }; stubs.runHelpers = { - handleRequires: sinon.stub().resolves({}), validateLegacyPlugin: sinon.stub() }; + stubs.handleRequires = { + handleRequires: sinon.stub().resolves({}) + }; + stubs.plugin = { aggregateRootHooks: sinon.stub().resolves() }; @@ -67,6 +70,7 @@ describe('worker', function () { '../../lib/mocha': stubs.Mocha, '../../lib/nodejs/serializer': stubs.serializer, '../../lib/cli/run-helpers': stubs.runHelpers, + '../../lib/cli/handle-requires': stubs.handleRequires, '../../lib/plugin-loader': stubs.plugin }); }); @@ -148,7 +152,7 @@ describe('worker', function () { serializeJavascript({require: 'foo'}) ); expect( - stubs.runHelpers.handleRequires, + stubs.handleRequires.handleRequires, 'to have a call satisfying', [ 'foo', @@ -217,9 +221,11 @@ describe('worker', function () { await worker.run('some-other-file.js'); expect(stubs.runHelpers, 'to satisfy', { - handleRequires: expect.it('was called once'), validateLegacyPlugin: expect.it('was called once') }); + expect(stubs.handleRequires, 'to satisfy', { + handleRequires: expect.it('was called once') + }); }); }); });