Skip to content

Commit 6d5ddb5

Browse files
committed
Snapshot test CLI output.
1 parent 3a18f6e commit 6d5ddb5

19 files changed

+206
-62
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
package.json
2+
/test/snapshots

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Simplified the GitHub Actions CI config with the [`npm install-test`](https://docs.npmjs.com/cli/v7/commands/npm-install-test) command.
1313
- Removed `npm-debug.log` from the `.gitignore` file as npm [v4.2.0](https://github.com/npm/npm/releases/tag/v4.2.0)+ doesn’t create it in the current working directory.
1414
- Use the `FORCE_COLOR` environment variable in tests to ensure output is colorized.
15+
- Use a new [`snapshot-assertion`](https://npm.im/snapshot-assertion) dev dependency to snapshot test CLI output.
1516
- Improved internal JSDoc.
1617

1718
## 3.0.0

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"hard-rejection": "^2.1.0",
6060
"jsdoc-md": "^8.0.0",
6161
"prettier": "^2.2.1",
62+
"snapshot-assertion": "^2.0.0",
6263
"test-director": "^5.0.0"
6364
},
6465
"scripts": {

test/cli/coverage-node.test.js

Lines changed: 131 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
const { strictEqual } = require('assert');
44
const fs = require('fs');
5-
const { join, relative } = require('path');
5+
const { join, relative, resolve } = require('path');
66
const { disposableDirectory } = require('disposable-directory');
7+
const snapshot = require('snapshot-assertion');
78
const coverageSupported = require('../../public/coverageSupported');
8-
const minNodeVersion = require('../../public/coverageSupportedMinNodeVersion');
99
const execFilePromise = require('../execFilePromise');
1010
const stripStackTraces = require('../stripStackTraces');
1111

12-
const stdoutSkippedCodeCoverage = `\n\u001b[33mSkipped code coverage as Node.js is ${process.version}, v${minNodeVersion.major}.${minNodeVersion.minor}.${minNodeVersion.patch}+ is supported.\u001b[39m\n\n`;
12+
const SNAPSHOT_REPLACEMENT_FILE_PATH = '<file path>';
13+
const SNAPSHOT_REPLACEMENT_PROCESS_NODE_VERSION = '<process Node.js version>';
1314

1415
module.exports = (tests) => {
1516
tests.add('`coverage-node` CLI with 1 covered file.', async () => {
@@ -27,12 +28,24 @@ module.exports = (tests) => {
2728
}
2829
);
2930

30-
coverageSupported
31-
? strictEqual(
32-
stdout.replace(relative('', filePath), '<path>'),
33-
'\n\u001b[32m1 file covered:\u001b[39m\n\n <path>\n\n\u001b[1m\u001b[32m1/1 files covered.\u001b[22m\u001b[39m\n\n'
34-
)
35-
: strictEqual(stdout, stdoutSkippedCodeCoverage);
31+
await snapshot(
32+
coverageSupported
33+
? stdout.replace(
34+
relative('', filePath),
35+
SNAPSHOT_REPLACEMENT_FILE_PATH
36+
)
37+
: stdout.replace(
38+
process.version,
39+
SNAPSHOT_REPLACEMENT_PROCESS_NODE_VERSION
40+
),
41+
resolve(
42+
__dirname,
43+
`../snapshots/coverage-node/1-covered-file-coverage-${
44+
coverageSupported ? 'supported' : 'unsupported'
45+
}-stdout.ans`
46+
)
47+
);
48+
3649
strictEqual(stderr, '');
3750
});
3851
});
@@ -56,12 +69,24 @@ module.exports = (tests) => {
5669
}
5770
);
5871

59-
coverageSupported
60-
? strictEqual(
61-
stdout.replace(relative('', filePath), '<path>'),
62-
'\n\u001b[33m1 file ignoring coverage:\u001b[39m\n\n <path>:2:1 → 2:8\n\n\u001b[1m\u001b[33m0/1 files covered.\u001b[22m\u001b[39m\n\n'
63-
)
64-
: strictEqual(stdout, stdoutSkippedCodeCoverage);
72+
await snapshot(
73+
coverageSupported
74+
? stdout.replace(
75+
relative('', filePath),
76+
SNAPSHOT_REPLACEMENT_FILE_PATH
77+
)
78+
: stdout.replace(
79+
process.version,
80+
SNAPSHOT_REPLACEMENT_PROCESS_NODE_VERSION
81+
),
82+
resolve(
83+
__dirname,
84+
`../snapshots/coverage-node/1-ignored-file-coverage-${
85+
coverageSupported ? 'supported' : 'unsupported'
86+
}-stdout.ans`
87+
)
88+
);
89+
6590
strictEqual(stderr, '');
6691
});
6792
});
@@ -91,21 +116,35 @@ module.exports = (tests) => {
91116
({ stdout, stderr } = error);
92117
}
93118

94-
if (coverageSupported) {
95-
strictEqual(threw, true, 'CLI should error.');
96-
strictEqual(
97-
stdout,
98-
'\n\u001b[1m\u001b[31m0/1 files covered.\u001b[22m\u001b[39m\n\n'
99-
);
100-
strictEqual(
101-
stderr.replace(relative('', filePath), '<path>'),
102-
'\n\u001b[31m1 file missing coverage:\u001b[39m\n\n <path>:1:1 → 1:8\n'
119+
strictEqual(threw, coverageSupported ? true : undefined);
120+
121+
await snapshot(
122+
coverageSupported
123+
? stdout
124+
: stdout.replace(
125+
process.version,
126+
SNAPSHOT_REPLACEMENT_PROCESS_NODE_VERSION
127+
),
128+
resolve(
129+
__dirname,
130+
`../snapshots/coverage-node/1-uncovered-file-coverage-${
131+
coverageSupported ? 'supported' : 'unsupported'
132+
}-stdout.ans`
133+
)
134+
);
135+
136+
if (coverageSupported)
137+
await snapshot(
138+
stderr.replace(
139+
relative('', filePath),
140+
SNAPSHOT_REPLACEMENT_FILE_PATH
141+
),
142+
resolve(
143+
__dirname,
144+
'../snapshots/coverage-node/1-uncovered-file-coverage-supported-stderr.ans'
145+
)
103146
);
104-
} else {
105-
strictEqual(threw, undefined, 'CLI shouldn’t error.');
106-
strictEqual(stdout, stdoutSkippedCodeCoverage);
107-
strictEqual(stderr, '');
108-
}
147+
else strictEqual(stderr, '');
109148
});
110149
});
111150

@@ -162,27 +201,38 @@ require('${fileFPath}')`
162201
({ stdout, stderr } = error);
163202
}
164203

165-
if (coverageSupported) {
166-
strictEqual(threw, true, 'CLI should error.');
167-
strictEqual(
168-
stdout
169-
.replace(relative('', fileAPath), '<pathA>')
170-
.replace(relative('', fileBPath), '<pathB>')
171-
.replace(relative('', fileCPath), '<pathC>')
172-
.replace(relative('', fileDPath), '<pathD>'),
173-
'\n\u001b[32m2 files covered:\u001b[39m\n\n <pathA>\n <pathB>\n\n\u001b[33m2 files ignoring coverage:\u001b[39m\n\n <pathC>:2:1 → 2:8\n <pathD>:2:1 → 2:8\n\n\u001b[1m\u001b[31m2/6 files covered.\u001b[22m\u001b[39m\n\n'
174-
);
175-
strictEqual(
204+
strictEqual(threw, coverageSupported ? true : undefined);
205+
206+
await snapshot(
207+
coverageSupported
208+
? stdout
209+
.replace(relative('', fileAPath), '<pathA>')
210+
.replace(relative('', fileBPath), '<pathB>')
211+
.replace(relative('', fileCPath), '<pathC>')
212+
.replace(relative('', fileDPath), '<pathD>')
213+
: stdout.replace(
214+
process.version,
215+
SNAPSHOT_REPLACEMENT_PROCESS_NODE_VERSION
216+
),
217+
resolve(
218+
__dirname,
219+
`../snapshots/coverage-node/2-covered-ignored-uncovered-files-coverage-${
220+
coverageSupported ? 'supported' : 'unsupported'
221+
}-stdout.ans`
222+
)
223+
);
224+
225+
if (coverageSupported)
226+
await snapshot(
176227
stderr
177228
.replace(relative('', fileEPath), '<pathE>')
178229
.replace(relative('', fileFPath), '<pathF>'),
179-
'\n\u001b[31m2 files missing coverage:\u001b[39m\n\n <pathE>:1:1 → 1:8\n <pathF>:1:1 → 1:8\n'
230+
resolve(
231+
__dirname,
232+
'../snapshots/coverage-node/2-covered-ignored-uncovered-files-coverage-supported-stderr.ans'
233+
)
180234
);
181-
} else {
182-
strictEqual(threw, undefined, 'CLI shouldn’t error.');
183-
strictEqual(stdout, stdoutSkippedCodeCoverage);
184-
strictEqual(stderr, '');
185-
}
235+
else strictEqual(stderr, '');
186236
});
187237
}
188238
);
@@ -202,12 +252,23 @@ require('${fileFPath}')`
202252
}
203253
);
204254

205-
if (coverageSupported)
206-
strictEqual(
207-
stdout.replace(relative('', filePath), '<path>'),
208-
'Message.\n\n\u001b[32m1 file covered:\u001b[39m\n\n <path>\n\n\u001b[1m\u001b[32m1/1 files covered.\u001b[22m\u001b[39m\n\n'
209-
);
210-
else strictEqual(stdout, `Message.\n${stdoutSkippedCodeCoverage}`);
255+
await snapshot(
256+
coverageSupported
257+
? stdout.replace(
258+
relative('', filePath),
259+
SNAPSHOT_REPLACEMENT_FILE_PATH
260+
)
261+
: stdout.replace(
262+
process.version,
263+
SNAPSHOT_REPLACEMENT_PROCESS_NODE_VERSION
264+
),
265+
resolve(
266+
__dirname,
267+
`../snapshots/coverage-node/script-console-log-coverage-${
268+
coverageSupported ? 'supported' : 'unsupported'
269+
}-stdout.ans`
270+
)
271+
);
211272

212273
strictEqual(stderr, '');
213274
});
@@ -232,11 +293,15 @@ require('${fileFPath}')`
232293
var { stdout, stderr } = error;
233294
}
234295

235-
strictEqual(threw, true, 'CLI should error.');
296+
strictEqual(threw, true);
236297
strictEqual(stdout, '');
237-
strictEqual(
238-
stripStackTraces(stderr).replace(filePath, '<path>'),
239-
"<path>:1\nthrow new Error('Error.')\n^\n\nError: Error.\n"
298+
299+
await snapshot(
300+
stripStackTraces(stderr).replace(
301+
filePath,
302+
SNAPSHOT_REPLACEMENT_FILE_PATH
303+
),
304+
resolve(__dirname, '../snapshots/coverage-node/script-error-stderr.ans')
240305
);
241306
});
242307
});
@@ -269,7 +334,7 @@ deprecated()`
269334
var { stdout, stderr } = error;
270335
}
271336

272-
strictEqual(threw, true, 'CLI should error.');
337+
strictEqual(threw, true);
273338
strictEqual(stdout, '');
274339
strictEqual(stderr.includes('DeprecationWarning: Deprecated!'), true);
275340
});
@@ -290,13 +355,17 @@ deprecated()`
290355
var { stdout, stderr } = error;
291356
}
292357

293-
strictEqual(threw, true, 'CLI should error.');
358+
strictEqual(threw, true);
294359
strictEqual(stdout, '');
295-
strictEqual(
360+
361+
await snapshot(
296362
stripStackTraces(stderr),
297-
`Error running Node.js${
298-
coverageSupported ? ' with coverage' : ''
299-
}:\n Error: Node.js CLI arguments are required.\n`
363+
resolve(
364+
__dirname,
365+
`../snapshots/coverage-node/without-arguments-coverage-${
366+
coverageSupported ? 'supported' : 'unsupported'
367+
}-stderr.ans`
368+
)
300369
);
301370
});
302371
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
1 file covered:
3+
4+
<file path>
5+
6+
1/1 files covered.
7+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
Skipped code coverage as Node.js is <process Node.js version>, v13.3.0+ is supported.
3+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
1 file ignoring coverage:
3+
4+
<file path>:2:1 → 2:8
5+
6+
0/1 files covered.
7+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
Skipped code coverage as Node.js is <process Node.js version>, v13.3.0+ is supported.
3+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
1 file missing coverage:
3+
4+
<file path>:1:1 → 1:8
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
0/1 files covered.
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
Skipped code coverage as Node.js is <process Node.js version>, v13.3.0+ is supported.
3+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
2 files missing coverage:
3+
4+
<pathE>:1:1 → 1:8
5+
<pathF>:1:1 → 1:8
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
2 files covered:
3+
4+
<pathA>
5+
<pathB>
6+
7+
2 files ignoring coverage:
8+
9+
<pathC>:2:1 → 2:8
10+
<pathD>:2:1 → 2:8
11+
12+
2/6 files covered.
13+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
Skipped code coverage as Node.js is <process Node.js version>, v13.3.0+ is supported.
3+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Message.
2+
3+
1 file covered:
4+
5+
<file path>
6+
7+
1/1 files covered.
8+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Message.
2+
3+
Skipped code coverage as Node.js is <process Node.js version>, v13.3.0+ is supported.
4+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<file path>:1
2+
throw new Error('Error.')
3+
^
4+
5+
Error: Error.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Error running Node.js with coverage:
2+
Error: Node.js CLI arguments are required.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Error running Node.js:
2+
Error: Node.js CLI arguments are required.

0 commit comments

Comments
 (0)