From 2385d8813ffe8d4c1401abddd6213af5be31fdb2 Mon Sep 17 00:00:00 2001 From: Harry Wolff Date: Wed, 20 Dec 2017 17:55:41 -0500 Subject: [PATCH] Add support for addFiles config parameter. --- README.md | 16 ++++++++++++++-- src/runMocha.js | 3 ++- src/utils/getMochaOptions.js | 9 +++++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3df4a8c..aa69cda 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,10 @@ In `package.json` "jest-runner-mocha": { "cliOptions": { // Options here - } + }, + "addFiles": [ + // Absolute file paths + ], } } ``` @@ -71,7 +74,10 @@ or in `jest-runner-mocha.config.js` module.exports = { cliOptions: { // Options here - } + }, + addFiles: [ + // Absolute file paths + ], } ``` @@ -86,6 +92,12 @@ jest-runner-mocha maps some mocha CLI arguments to config options. For example ` |timeout|`"timeout": 10000` |compiler|`"compiler": "./path/to/babel-register"` +### addFiles + +If you want to include additional files (for example additional Mocha setup code), you can list the files that you want added. + +This value can be a string or an array of absolute file paths. + ### Coverage Coverage works outside of the box, simply `yarn jest -- --coverage` diff --git a/src/runMocha.js b/src/runMocha.js index 70a5226..b75559a 100644 --- a/src/runMocha.js +++ b/src/runMocha.js @@ -4,7 +4,7 @@ const setupCollectCoverage = require('./utils/setupCollectCoverage'); const getMochaOptions = require('./utils/getMochaOptions'); const runMocha = ({ config, testPath, globalConfig }, workerCallback) => { - const { cliOptions: mochaOptions } = getMochaOptions(config); + const { cliOptions: mochaOptions, addFiles } = getMochaOptions(config); class Reporter extends Mocha.reporters.Base { constructor(runner) { @@ -56,6 +56,7 @@ const runMocha = ({ config, testPath, globalConfig }, workerCallback) => { coveragePathIgnorePatterns: config.coveragePathIgnorePatterns, }); + addFiles.forEach(file => mocha.addFile(file)); mocha.addFile(testPath); const onEnd = () => { diff --git a/src/utils/getMochaOptions.js b/src/utils/getMochaOptions.js index cfd2e7d..c7d4d6e 100644 --- a/src/utils/getMochaOptions.js +++ b/src/utils/getMochaOptions.js @@ -3,14 +3,19 @@ const cosmiconfig = require('cosmiconfig'); const explorer = cosmiconfig('jest-runner-mocha', { sync: true }); -const normalize = (jestConfig, { cliOptions: rawCliOptions = {} }) => { +const normalize = ( + jestConfig, + { cliOptions: rawCliOptions = {}, addFiles: rawAddFiles = [] }, +) => { const cliOptions = Object.assign({}, rawCliOptions); if (cliOptions.compiler && !path.isAbsolute(cliOptions.compiler)) { cliOptions.compiler = path.resolve(jestConfig.rootDir, cliOptions.compiler); } - return { cliOptions }; + const addFiles = [].concat(rawAddFiles); + + return { cliOptions, addFiles }; }; const getMochaOptions = jestConfig => {