diff --git a/bin/c8.js b/bin/c8.js index bbd4220c..ad0ca7f2 100755 --- a/bin/c8.js +++ b/bin/c8.js @@ -18,8 +18,10 @@ if (argv._[0] === 'report') { argv = yargs.parse(process.argv) // support flag arguments after "report". outputReport() } else { - rimraf.sync(argv.tempDirectory) - mkdirp.sync(argv.tempDirectory) + if (argv.clean) { + rimraf.sync(argv.tempDirectory) + mkdirp.sync(argv.tempDirectory) + } process.env.NODE_V8_COVERAGE = argv.tempDirectory foreground(hideInstrumenterArgs(argv), () => { diff --git a/lib/parse-args.js b/lib/parse-args.js index 1c629264..6d43a2fb 100644 --- a/lib/parse-args.js +++ b/lib/parse-args.js @@ -24,10 +24,6 @@ yargs() default: [], describe: 'a list of specific files that should be covered (glob patterns are supported)' }) - .option('coverage-directory', { - default: './coverage', - describe: 'directory to output coverage JSON and reports' - }) .option('temp-directory', { default: './coverage/tmp', describe: 'directory V8 coverage data is written to and read from' @@ -41,6 +37,11 @@ yargs() type: 'boolean', describe: 'omit any paths that are not absolute, e.g., internal/net.js' }) + .option('clean', { + default: true, + type: 'boolean', + describe: 'should temp files be deleted before script execution' + }) .command('report', 'read V8 coverage data from temp and output report') .pkgConf('c8') .config(config) diff --git a/package.json b/package.json index 2a6afef2..a60a3b30 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ }, "scripts": { "test": "node ./bin/c8.js --reporter=html --reporter=text mocha ./test/*.js", + "test:snap": "CHAI_JEST_SNAPSHOT_UPDATE_ALL=true npm test", "posttest": "standard", "coverage": "./bin/c8.js report --reporter=text-lcov | coveralls", "release": "standard-version" @@ -47,6 +48,7 @@ }, "devDependencies": { "chai": "^4.1.2", + "chai-jest-snapshot": "^2.0.0", "coveralls": "^3.0.2", "mocha": "^5.2.0", "standard": "^12.0.1", diff --git a/test/integration.js b/test/integration.js index 6c1f1d01..dd91bc15 100644 --- a/test/integration.js +++ b/test/integration.js @@ -1,44 +1,37 @@ -/* global describe, it */ +/* global describe, beforeEach, it */ const { spawnSync } = require('child_process') const c8Path = require.resolve('../bin/c8') const nodePath = process.execPath +const chaiJestSnapshot = require('chai-jest-snapshot') -require('chai').should() +require('chai') + .use(chaiJestSnapshot) + .should() + +beforeEach(function () { chaiJestSnapshot.configureUsingMochaContext(this) }) describe('c8', () => { it('reports coverage for script that exits normally', () => { const { output } = spawnSync(nodePath, [ c8Path, '--exclude="test/*.js"', + '--clean=false', nodePath, require.resolve('./fixtures/normal') ]) - output.toString('utf8').should.include(` ------------|----------|----------|----------|----------|-------------------| -File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ------------|----------|----------|----------|----------|-------------------| -All files | 91.18 | 88.89 | 0 | 91.18 | | - async.js | 100 | 100 | 100 | 100 | | - normal.js | 85.71 | 75 | 0 | 85.71 | 14,15,16 | ------------|----------|----------|----------|----------|-------------------|`) + output.toString('utf8').should.matchSnapshot() }) it('merges reports from subprocesses together', () => { const { output } = spawnSync(nodePath, [ c8Path, '--exclude="test/*.js"', + '--clean=false', nodePath, require.resolve('./fixtures/multiple-spawn') ]) - output.toString('utf8').should.include(` --------------------|----------|----------|----------|----------|-------------------| -File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | --------------------|----------|----------|----------|----------|-------------------| -All files | 100 | 77.78 | 100 | 100 | | - multiple-spawn.js | 100 | 100 | 100 | 100 | | - subprocess.js | 100 | 71.43 | 100 | 100 | 9,13 | --------------------|----------|----------|----------|----------|-------------------|`) + output.toString('utf8').should.matchSnapshot() }) it('omit-relative can be set to false', () => { @@ -46,6 +39,7 @@ All files | 100 | 77.78 | 100 | 100 | c8Path, '--exclude="test/*.js"', '--omit-relative=false', + '--clean=false', nodePath, require.resolve('./fixtures/multiple-spawn') ]) diff --git a/test/integration.js.snap b/test/integration.js.snap new file mode 100644 index 00000000..788499ec --- /dev/null +++ b/test/integration.js.snap @@ -0,0 +1,43 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`c8 merges reports from subprocesses together 1`] = ` +",first + +second + +--------------------|----------|----------|----------|----------|-------------------| +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | +--------------------|----------|----------|----------|----------|-------------------| +All files | 94.12 | 70.59 | 0 | 94.12 | | + bin | 83.72 | 57.14 | 100 | 83.72 | | + c8.js | 83.72 | 57.14 | 100 | 83.72 |... 22,40,41,42,43 | + lib | 96.41 | 65.38 | 100 | 96.41 | | + parse-args.js | 97.47 | 44.44 | 100 | 97.47 | 55,56 | + report.js | 95.45 | 76.47 | 100 | 95.45 | 51,52,53,54 | + test/fixtures | 95.16 | 83.33 | 0 | 95.16 | | + async.js | 100 | 100 | 100 | 100 | | + multiple-spawn.js | 100 | 100 | 100 | 100 | | + normal.js | 85.71 | 75 | 0 | 85.71 | 14,15,16 | + subprocess.js | 100 | 71.43 | 100 | 100 | 9,13 | +--------------------|----------|----------|----------|----------|-------------------| +," +`; + +exports[`c8 reports coverage for script that exits normally 1`] = ` +",hey +i am a line of code +what +hey +what +hey +what +hey +-----------|----------|----------|----------|----------|-------------------| +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | +-----------|----------|----------|----------|----------|-------------------| +All files | 91.18 | 88.89 | 0 | 91.18 | | + async.js | 100 | 100 | 100 | 100 | | + normal.js | 85.71 | 75 | 0 | 85.71 | 14,15,16 | +-----------|----------|----------|----------|----------|-------------------| +," +`;