.apply((context, args) => { var coverage = args.coverage; var reporter = args.reporter; var shouldOpen = args.open; var moreOptions = args._.slice(1); var env = { babelConfig: JSON.stringify(context.babelConfig), coverage: coverage ? 'enable' : 'disable' }; var NYC = require('nyc'); if (coverage) { require('babel-register')(context.babelConfig); var nyc = new NYC({ extensions: ['.js', '.jsx'], exclude: ['dist', 'test', 'coverage'], include: ['src'] }); nyc.reset(); nyc.addAllFiles(); } var sw = require('spawn-wrap'); var foreground = require('foreground-child'); var testDir = path.join(context.projectDir, 'test'); var wrapper = require.resolve('./wrap.js'); var mochaCmd = ['./node_modules/.bin/mocha', '--recursive']; if (moreOptions.length > 0) { mochaCmd.push(...moreOptions); } mochaCmd.push(testDir); sw([wrapper], env); foreground(mochaCmd, done => { if (coverage) { showCoverage(context, reporter, shouldOpen); } done(); }); });
// Support running nyc as a user without HOME (e.g. linux 'nobody'), // https://github.com/istanbuljs/nyc/issues/951 SPAWN_WRAP_SHIM_ROOT: process.env.SPAWN_WRAP_SHIM_ROOT || process.env.XDG_CACHE_HOME || require('os').homedir(), NYC_CONFIG: JSON.stringify(argv), NYC_CWD: process.cwd(), NYC_ROOT_ID: nyc.rootId, NYC_INSTRUMENTER: argv.instrumenter } if (argv['babel-cache'] === false) { // babel's cache interferes with some configurations, so is // disabled by default. opt in by setting babel-cache=true. env.BABEL_DISABLE_CACHE = process.env.BABEL_DISABLE_CACHE = '1' } sw([wrapper], env) // Both running the test script invocation and the check-coverage run may // set process.exitCode. Keep track so that both children are run, but // a non-zero exit codes in either one leads to an overall non-zero exit code. process.exitCode = 0 foreground(processArgs.hideInstrumenterArgs( // use the same argv descrption, but don't exit // for flags like --help. configUtil.buildYargs().parse(process.argv.slice(2)) ), function (done) { var mainChildExitCode = process.exitCode nyc.writeProcessIndex() nyc.maybePurgeSourceMapCache()
} else if (argv._.length) { // wrap subprocesses and execute argv[1] var nyc = (new NYC()) nyc.reset() if (argv.all) nyc.addAllFiles() if (!Array.isArray(argv.require)) argv.require = [argv.require] var env = { NYC_CWD: process.cwd(), NYC_CACHE: argv.cache ? 'enable' : 'disable' } if (argv.require.length) { env.NYC_REQUIRE = argv.require.join(',') } sw([__filename], env) foreground(nyc.mungeArgs(argv), function (done) { if (argv.checkCoverage) { checkCoverage(argv, function (done) { if (!argv.silent) report(argv) return done() }) } else { if (!argv.silent) report(argv) return done() } }) } else { // I don't have a clue what you're doing. yargs.showHelp()
'--functions=' + argv.functions, '--branches=' + argv.branches, '--statements=' + argv.statements, path.resolve(process.cwd(), './.nyc_output/*.json') ] ) } else if (argv._.length) { // wrap subprocesses and execute argv[1] var nyc = (new NYC()) nyc.cleanup() if (argv.all) nyc.addAllFiles() if (!Array.isArray(argv.require)) argv.require = [argv.require] sw([__filename], { NYC_CWD: process.cwd(), NYC_REQUIRE: argv.require.join(',') }) foreground(nyc.mungeArgs(argv), function (done) { if (!argv.silent) report(argv) return done() }) } else { // I don't have a clue what you're doing. yargs.showHelp() } } function report (argv) { process.env.NYC_CWD = process.cwd()
}) .help('h') .alias('h', 'help') .version(require('../package.json').version) .example('$0 npm test', 'instrument your tests with coverage') .example('$0 report --reporter=text-lcov', 'output lcov report after running your tests') .epilog('visit http://git.io/vTJJB for list of available reporters'), argv = yargs.argv if (argv._.length && ~argv._.indexOf('report')) { // run a report. process.env.NYC_CWD = process.cwd() ;(new NYC({ reporter: argv.reporter })).report() } else if (argv._.length) { // wrap subprocesses and execute argv[1] ;(new NYC()).cleanup() sw([__filename], { NYC_CWD: process.cwd() }) foreground(process.argv.slice(2)) } else { // I don't have a clue what you're doing. yargs.showHelp() } }