diff --git a/package.json b/package.json index ae9a141696..02971e9f47 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "build": "node scripts/build", "start": "node scripts/develop", "test": "node scripts/test", - "test-coverage": "nyc npm run test", + "test-coverage": "node scripts/test --coverage", "test-functional": "node scripts/test-functional", "publish-coverage": "nyc report --reporter=text-lcov | coveralls", "audit-deps": "node ./scripts/audit-deps", diff --git a/scripts/lib/mocha.js b/scripts/lib/mocha.js index 6ea6c5202c..31de88802b 100644 --- a/scripts/lib/mocha.js +++ b/scripts/lib/mocha.js @@ -4,11 +4,16 @@ const shell = require('shelljs'); const config = require('./config'); -// Get the explicit path to mocha (needed to make it find mocha binary on travis windows workers). -const mochaPath = String(shell.which('mocha')); - -const runMocha = (args, execMochaOptions = {}) => { - const res = spawnSync(mochaPath, args, { +// Get the explicit path (needed on travis windows workers). +function which(...args) { + return String(shell.which(...args)); +} + +const runMocha = (args, execMochaOptions = {}, coverageEnabled) => { + const mochaPath = which('mocha'); + const binArgs = coverageEnabled ? [mochaPath, ...args] : args; + const binPath = coverageEnabled ? which('nyc') : mochaPath; + const res = spawnSync(binPath, binArgs, { ...execMochaOptions, stdio: 'inherit', }); @@ -21,8 +26,8 @@ const runMocha = (args, execMochaOptions = {}) => { return res.status === 0; }; -exports.mochaUnit = (execMochaOptions) => { - return runMocha(config.mocha.unit, execMochaOptions); +exports.mochaUnit = (execMochaOptions, coverageEnabled) => { + return runMocha(config.mocha.unit, execMochaOptions, coverageEnabled); }; exports.mochaFunctional = (execMochaOptions) => { diff --git a/scripts/test b/scripts/test index df03d1eebc..383cbdad5a 100644 --- a/scripts/test +++ b/scripts/test @@ -1,5 +1,17 @@ #!/usr/bin/env node +let COVERAGE = process.argv.includes('--coverage'); + +const {TRAVIS_OS_NAME} = process.env; +if (TRAVIS_OS_NAME === 'windows') { + console.log( + 'Skipping coverage collection because running on', + TRAVIS_OS_NAME, + '(See issue https://github.com/mozilla/web-ext/issues/1739)' + ); + COVERAGE = false; +} + const eslint = require('./lib/eslint'); const {flowCheck} = require('./lib/flow'); const {mochaUnit} = require('./lib/mocha'); @@ -14,6 +26,6 @@ if (!flowCheck()) { process.exit(1); } -console.log('Running mocha unit tests...'); -const ok = mochaUnit(); +console.log('Running mocha unit tests...', COVERAGE ? '(COVERAGE)' : ''); +const ok = mochaUnit({}, COVERAGE); process.exit(ok ? 0 : 1);