@stryker-mutator/mocha-runner
Version:
A plugin to use the mocha test runner in Stryker, the JavaScript mutation testing framework
59 lines • 2.23 kB
JavaScript
import path from 'path';
import { createRequire } from 'module';
import { pathToFileURL } from 'url';
import { default as bundledMocha } from 'mocha';
const resolveMocha = async () => {
try {
const require = createRequire(path.join(process.cwd(), 'package.json'));
const mochaPath = require.resolve('mocha');
const mochaModule = await import(pathToFileURL(mochaPath).href);
return {
mocha: mochaModule.default ?? bundledMocha,
require,
mochaRoot: path.dirname(require.resolve('mocha/package.json')),
};
}
catch {
const require = createRequire(import.meta.url);
return {
mocha: bundledMocha,
require,
mochaRoot: path.dirname(require.resolve('mocha/package.json')),
};
}
};
const { mocha, require, mochaRoot } = await resolveMocha();
// https://github.com/mochajs/mocha/blob/master/lib/cli/run-helpers.js#L132
const runHelpers = require(`${mochaRoot}/lib/cli/run-helpers`);
let collectFiles;
async function resolveLoadOptions() {
try {
return require(`${mochaRoot}/lib/cli/options`).loadOptions;
}
catch {
// Since Mocha 12 the cli is distributed as ECMAScript modules
const options = await import(pathToFileURL(path.join(mochaRoot, 'lib/cli/options.mjs')).href);
return options.loadOptions;
}
}
const loadOptions = await resolveLoadOptions();
const handleRequires = runHelpers.handleRequires;
const loadRootHooks = runHelpers.loadRootHooks; // loadRootHooks is available since mocha v7.2 and removed again in 8.0
collectFiles = runHelpers.handleFiles;
if (!collectFiles) {
// Might be moved:
// https://github.com/mochajs/mocha/commit/15b96afccaf508312445770e3af1c145d90b28c6#diff-39b692a81eb0c9f3614247af744ab4a8
collectFiles = require(`${mochaRoot}/lib/cli/collect-files`);
}
/**
* Wraps Mocha class and require for testability
*/
export class LibWrapper {
static Mocha = mocha;
static require = require;
static loadOptions = loadOptions;
static collectFiles = collectFiles;
static handleRequires = handleRequires;
static loadRootHooks = loadRootHooks;
}
//# sourceMappingURL=lib-wrapper.js.map