Skip to content

Commit

Permalink
feat: improve test assertions
Browse files Browse the repository at this point in the history
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)
```
  • Loading branch information
laggingreflex committed Sep 13, 2018
1 parent 58c076e commit b486ff4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions test/integration.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/* global describe, it */

const assert = require('assert')
const { spawnSync } = require('child_process')
const c8Path = require.resolve('../bin/c8')
const nodePath = process.execPath

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

describe('c8', () => {
it('reports coverage for script that exits normally', () => {
Expand All @@ -15,7 +21,7 @@ describe('c8', () => {
nodePath,
require.resolve('./fixtures/normal')
])
output.toString('utf8').should.include(`
shouldInclude(output.toString('utf8'), `
-----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
Expand All @@ -33,7 +39,7 @@ All files | 91.18 | 88.89 | 0 | 91.18 | |
nodePath,
require.resolve('./fixtures/multiple-spawn')
])
output.toString('utf8').should.include(`
shouldInclude(output.toString('utf8'), `
--------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------------|----------|----------|----------|----------|-------------------|
Expand All @@ -60,7 +66,7 @@ All files | 94.12 | 70.59 | 0 | 94.12 |
nodePath,
require.resolve('./fixtures/multiple-spawn')
])
output.toString('utf8').should.match(
shouldInclude(output.toString('utf8'),
/Error: ENOENT: no such file or directory.*loaders\.js/
)
})
Expand Down

0 comments on commit b486ff4

Please sign in to comment.