UNPKG

@stryker-mutator/mocha-runner

Version:

A plugin to use the mocha test runner in Stryker, the JavaScript mutation testing framework

68 lines 2.37 kB
import path from 'path'; import fs from 'fs'; import { tokens, commonTokens } from '@stryker-mutator/api/plugin'; import { LibWrapper } from './lib-wrapper.js'; /** * A class that contains polyfills for different versions of mocha. * Tries to mimic the functionality of mocha's latest api. * * Currently supports mocha < 6 */ export class MochaAdapter { log; static inject = tokens(commonTokens.logger); constructor(log) { this.log = log; } create(options) { return new LibWrapper.Mocha(options); } collectFiles(options) { const originalProcessExit = process.exit; try { // process.exit unfortunate side effect: https://github.com/mochajs/mocha/blob/07ea8763c663bdd3fe1f8446cdb62dae233f4916/lib/cli/run-helpers.js#L174 process.exit = () => { }; const files = LibWrapper.collectFiles(options); if (Array.isArray(files)) { return files; } else { return files.files; } } finally { process.exit = originalProcessExit; } } async handleRequires(requires) { this.log.trace('Resolving requires %s', requires); if (LibWrapper.handleRequires) { this.log.trace('Using `handleRequires`'); const rawRootHooks = await LibWrapper.handleRequires(requires); if (rawRootHooks) { if (LibWrapper.loadRootHooks) { // `loadRootHooks` made a brief appearance in mocha 8, removed in mocha 8.2 return await LibWrapper.loadRootHooks(rawRootHooks); } else { return rawRootHooks.rootHooks; } } } else { const modulesToRequire = requires.map((moduleName) => { const maybeSetupFileName = path.resolve(moduleName); if (fs.existsSync(maybeSetupFileName)) { return maybeSetupFileName; } else { return moduleName; } }); this.log.trace('Requiring %s manually', modulesToRequire); modulesToRequire.forEach(LibWrapper.require); } return undefined; } } //# sourceMappingURL=mocha-adapter.js.map