Skip to content

Commit

Permalink
refactor: extract handleRequires() to file
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartones authored and Diego Muñoz Pérez committed Jun 3, 2024
1 parent 472a8be commit 2615a18
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 45 deletions.
38 changes: 38 additions & 0 deletions lib/cli/handle-requires.js
Original file line number Diff line number Diff line change
@@ -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 <require>`.
*
* Returns array of `mochaHooks` exports, if any.
* @param {string[]} requires - Modules to require
* @returns {Promise<object>} 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;
};
35 changes: 0 additions & 35 deletions lib/cli/run-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 <require>`.
*
* Returns array of `mochaHooks` exports, if any.
* @param {string[]} requires - Modules to require
* @returns {Promise<object>} 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
Expand Down
8 changes: 2 additions & 6 deletions lib/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion lib/nodejs/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
12 changes: 9 additions & 3 deletions test/node-unit/worker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
};
Expand All @@ -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
});
});
Expand Down Expand Up @@ -148,7 +152,7 @@ describe('worker', function () {
serializeJavascript({require: 'foo'})
);
expect(
stubs.runHelpers.handleRequires,
stubs.handleRequires.handleRequires,
'to have a call satisfying',
[
'foo',
Expand Down Expand Up @@ -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')
});
});
});
});
Expand Down

0 comments on commit 2615a18

Please sign in to comment.