Skip to content

Commit 277c9d0

Browse files
laggingreflexbcoe
authored andcommitted
feat: improve test assertions (bcoe#28)
1 parent 39ebad6 commit 277c9d0

File tree

5 files changed

+66
-24
lines changed

5 files changed

+66
-24
lines changed

bin/c8.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ if (argv._[0] === 'report') {
1818
argv = yargs.parse(process.argv) // support flag arguments after "report".
1919
outputReport()
2020
} else {
21-
rimraf.sync(argv.tempDirectory)
22-
mkdirp.sync(argv.tempDirectory)
21+
if (argv.clean) {
22+
rimraf.sync(argv.tempDirectory)
23+
mkdirp.sync(argv.tempDirectory)
24+
}
2325
process.env.NODE_V8_COVERAGE = argv.tempDirectory
2426

2527
foreground(hideInstrumenterArgs(argv), () => {

lib/parse-args.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ yargs()
2424
default: [],
2525
describe: 'a list of specific files that should be covered (glob patterns are supported)'
2626
})
27-
.option('coverage-directory', {
28-
default: './coverage',
29-
describe: 'directory to output coverage JSON and reports'
30-
})
3127
.option('temp-directory', {
3228
default: './coverage/tmp',
3329
describe: 'directory V8 coverage data is written to and read from'
@@ -41,6 +37,11 @@ yargs()
4137
type: 'boolean',
4238
describe: 'omit any paths that are not absolute, e.g., internal/net.js'
4339
})
40+
.option('clean', {
41+
default: true,
42+
type: 'boolean',
43+
describe: 'should temp files be deleted before script execution'
44+
})
4445
.command('report', 'read V8 coverage data from temp and output report')
4546
.pkgConf('c8')
4647
.config(config)

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"scripts": {
1212
"test": "node ./bin/c8.js --reporter=html --reporter=text mocha ./test/*.js",
13+
"test:snap": "CHAI_JEST_SNAPSHOT_UPDATE_ALL=true npm test",
1314
"posttest": "standard",
1415
"coverage": "./bin/c8.js report --reporter=text-lcov | coveralls",
1516
"release": "standard-version"
@@ -47,6 +48,7 @@
4748
},
4849
"devDependencies": {
4950
"chai": "^4.1.2",
51+
"chai-jest-snapshot": "^2.0.0",
5052
"coveralls": "^3.0.2",
5153
"mocha": "^5.2.0",
5254
"standard": "^12.0.1",

test/integration.js

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,45 @@
1-
/* global describe, it */
1+
/* global describe, beforeEach, it */
22

33
const { spawnSync } = require('child_process')
44
const c8Path = require.resolve('../bin/c8')
55
const nodePath = process.execPath
6+
const chaiJestSnapshot = require('chai-jest-snapshot')
67

7-
require('chai').should()
8+
require('chai')
9+
.use(chaiJestSnapshot)
10+
.should()
11+
12+
beforeEach(function () { chaiJestSnapshot.configureUsingMochaContext(this) })
813

914
describe('c8', () => {
1015
it('reports coverage for script that exits normally', () => {
1116
const { output } = spawnSync(nodePath, [
1217
c8Path,
1318
'--exclude="test/*.js"',
19+
'--clean=false',
1420
nodePath,
1521
require.resolve('./fixtures/normal')
1622
])
17-
output.toString('utf8').should.include(`
18-
-----------|----------|----------|----------|----------|-------------------|
19-
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
20-
-----------|----------|----------|----------|----------|-------------------|
21-
All files | 91.18 | 88.89 | 0 | 91.18 | |
22-
async.js | 100 | 100 | 100 | 100 | |
23-
normal.js | 85.71 | 75 | 0 | 85.71 | 14,15,16 |
24-
-----------|----------|----------|----------|----------|-------------------|`)
23+
output.toString('utf8').should.matchSnapshot()
2524
})
2625

2726
it('merges reports from subprocesses together', () => {
2827
const { output } = spawnSync(nodePath, [
2928
c8Path,
3029
'--exclude="test/*.js"',
30+
'--clean=false',
3131
nodePath,
3232
require.resolve('./fixtures/multiple-spawn')
3333
])
34-
output.toString('utf8').should.include(`
35-
-------------------|----------|----------|----------|----------|-------------------|
36-
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
37-
-------------------|----------|----------|----------|----------|-------------------|
38-
All files | 100 | 77.78 | 100 | 100 | |
39-
multiple-spawn.js | 100 | 100 | 100 | 100 | |
40-
subprocess.js | 100 | 71.43 | 100 | 100 | 9,13 |
41-
-------------------|----------|----------|----------|----------|-------------------|`)
34+
output.toString('utf8').should.matchSnapshot()
4235
})
4336

4437
it('omit-relative can be set to false', () => {
4538
const { output } = spawnSync(nodePath, [
4639
c8Path,
4740
'--exclude="test/*.js"',
4841
'--omit-relative=false',
42+
'--clean=false',
4943
nodePath,
5044
require.resolve('./fixtures/multiple-spawn')
5145
])

test/integration.js.snap

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`c8 merges reports from subprocesses together 1`] = `
4+
",first
5+
6+
second
7+
8+
--------------------|----------|----------|----------|----------|-------------------|
9+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
10+
--------------------|----------|----------|----------|----------|-------------------|
11+
All files | 94.12 | 70.59 | 0 | 94.12 | |
12+
bin | 83.72 | 57.14 | 100 | 83.72 | |
13+
c8.js | 83.72 | 57.14 | 100 | 83.72 |... 22,40,41,42,43 |
14+
lib | 96.41 | 65.38 | 100 | 96.41 | |
15+
parse-args.js | 97.47 | 44.44 | 100 | 97.47 | 55,56 |
16+
report.js | 95.45 | 76.47 | 100 | 95.45 | 51,52,53,54 |
17+
test/fixtures | 95.16 | 83.33 | 0 | 95.16 | |
18+
async.js | 100 | 100 | 100 | 100 | |
19+
multiple-spawn.js | 100 | 100 | 100 | 100 | |
20+
normal.js | 85.71 | 75 | 0 | 85.71 | 14,15,16 |
21+
subprocess.js | 100 | 71.43 | 100 | 100 | 9,13 |
22+
--------------------|----------|----------|----------|----------|-------------------|
23+
,"
24+
`;
25+
26+
exports[`c8 reports coverage for script that exits normally 1`] = `
27+
",hey
28+
i am a line of code
29+
what
30+
hey
31+
what
32+
hey
33+
what
34+
hey
35+
-----------|----------|----------|----------|----------|-------------------|
36+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
37+
-----------|----------|----------|----------|----------|-------------------|
38+
All files | 91.18 | 88.89 | 0 | 91.18 | |
39+
async.js | 100 | 100 | 100 | 100 | |
40+
normal.js | 85.71 | 75 | 0 | 85.71 | 14,15,16 |
41+
-----------|----------|----------|----------|----------|-------------------|
42+
,"
43+
`;

0 commit comments

Comments
 (0)