Skip to content

Commit

Permalink
feat: improve test assertions (bcoe#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
laggingreflex authored and bcoe committed Sep 16, 2018
1 parent 39ebad6 commit 277c9d0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
6 changes: 4 additions & 2 deletions bin/c8.js
Original file line number Diff line number Diff line change
Expand Up @@ -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), () => {
Expand Down
9 changes: 5 additions & 4 deletions lib/parse-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
30 changes: 12 additions & 18 deletions test/integration.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,45 @@
/* 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', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--omit-relative=false',
'--clean=false',
nodePath,
require.resolve('./fixtures/multiple-spawn')
])
Expand Down
43 changes: 43 additions & 0 deletions test/integration.js.snap
Original file line number Diff line number Diff line change
@@ -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 |
-----------|----------|----------|----------|----------|-------------------|
,"
`;

0 comments on commit 277c9d0

Please sign in to comment.