Skip to content

Commit b486ff4

Browse files
committed
feat: improve test assertions
When a test failed, it'd show an output like this which is hard to look at: ``` 1) c8 merges reports from subprocesses together: AssertionError: expected ',first\n\nsecond\n\n--------------------|----------|----------|----------|----------|-------------------|\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |\n--------------------|----------|----------|----------|----------|---------- ---------|\nAll files | 94.12 | 73.08 | 0 | 94.12 | |\n bin | 83.72 | 57.14 | 10 0 | 83.72 | |\n c8.js | 83.72 | 57.14 | 100 | 83.72 |... 22,40,41,42,43 |\n lib | 96 .41 | 65.38 | 100 | 96.41 | |\n parse-args.js | 97.47 | 44.44 | 100 | 97.47 | 55,56 |\n r eport.js | 95.45 | 76.47 | 100 | 95.45 | 51,52,53,54 |\n test/fixtures | 95.16 | 89.47 | 0 | 95.16 | |\n async.js | 100 | 100 | 100 | 100 | |\n multiple-spawn.js | 100 | 100 | 100 | 100 | |\n normal.js | 85.71 | 75 | 0 | 85.71 | 14,15,16 |\n subprocess.js | 100 | 87.5 | 100 | 100 | 9 |\n--------------------|----------|----------|----------|----------|---------------- ---|\n,' to include '\n--------------------|----------|----------|----------|----------|-------------------|\nFile | % Stmts | % Branc h | % Funcs | % Lines | Uncovered Line #s |\n--------------------|----------|----------|----------|----------|-------------------|\nAll files | 94.12 | 70.59 | 0 | 94.12 | |\n bin | 83.72 | 57.14 | 100 | 83.72 | |\n c8.js | 83.72 | 57.14 | 100 | 83.72 |... 22,40,41,42,43 |\n lib | 96.41 | 65.38 | 100 | 96.41 | |\n parse-args.js | 97.47 | 44.44 | 100 | 97.47 | 55,56 |\n report.js | 95.45 | 76.47 | 100 | 95.45 | 51,52,53,54 |\n test/fixtures | 95.16 | 83.33 | 0 | 95.16 | |\n async .js | 100 | 100 | 100 | 100 | |\n multiple-spawn.js | 100 | 100 | 100 | 100 | |\n normal.js | 85.71 | 75 | 0 | 85.71 | 14,15,16 |\n subprocess.js | 100 | 71.43 | 100 | 100 | 9,13 |\n--------------------|----------|----------|----------|----------|-------------------|' at Context.it (test\integration.js:36:36) ``` This commit tries to improve the error message: ``` 1) c8 merges reports from subprocesses together: AssertionError [ERR_ASSERTION]: ',first\n\nsecond\n\n--------------------|----------|----------|----------|----------|-------------------|\nFile ... deepEqual '\n--------------------|----------|----------|----------|----------|-------------------|\nFile | % Stmts | % ... + expected - actual -,first -second - --------------------|----------|----------|----------|----------|-------------------| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | --------------------|----------|----------|----------|----------|-------------------| -All files | 94.12 | 73.08 | 0 | 94.12 | | +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 | 89.47 | 0 | 95.16 | | + 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 | 87.5 | 100 | 100 | 9 | ---------------------|----------|----------|----------|----------|-------------------| -, + subprocess.js | 100 | 71.43 | 100 | 100 | 9,13 | +--------------------|----------|----------|----------|----------|-------------------| at new AssertionError (internal/assert.js:268:11) ```
1 parent 58c076e commit b486ff4

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

test/integration.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
/* global describe, it */
22

3+
const assert = require('assert')
34
const { spawnSync } = require('child_process')
45
const c8Path = require.resolve('../bin/c8')
56
const nodePath = process.execPath
67

78
require('chai').should()
9+
const shouldInclude = (actual, expected) => {
10+
if (!actual[expected instanceof RegExp ? 'match' : 'includes'](expected)) {
11+
throw new assert.AssertionError({actual, expected, operator: 'deepEqual'})
12+
}
13+
}
814

915
describe('c8', () => {
1016
it('reports coverage for script that exits normally', () => {
@@ -15,7 +21,7 @@ describe('c8', () => {
1521
nodePath,
1622
require.resolve('./fixtures/normal')
1723
])
18-
output.toString('utf8').should.include(`
24+
shouldInclude(output.toString('utf8'), `
1925
-----------|----------|----------|----------|----------|-------------------|
2026
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
2127
-----------|----------|----------|----------|----------|-------------------|
@@ -33,7 +39,7 @@ All files | 91.18 | 88.89 | 0 | 91.18 | |
3339
nodePath,
3440
require.resolve('./fixtures/multiple-spawn')
3541
])
36-
output.toString('utf8').should.include(`
42+
shouldInclude(output.toString('utf8'), `
3743
--------------------|----------|----------|----------|----------|-------------------|
3844
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
3945
--------------------|----------|----------|----------|----------|-------------------|
@@ -60,7 +66,7 @@ All files | 94.12 | 70.59 | 0 | 94.12 |
6066
nodePath,
6167
require.resolve('./fixtures/multiple-spawn')
6268
])
63-
output.toString('utf8').should.match(
69+
shouldInclude(output.toString('utf8'),
6470
/Error: ENOENT: no such file or directory.*loaders\.js/
6571
)
6672
})

0 commit comments

Comments
 (0)