2
2
3
3
const { strictEqual } = require ( 'assert' ) ;
4
4
const fs = require ( 'fs' ) ;
5
- const { join, relative } = require ( 'path' ) ;
5
+ const { join, relative, resolve } = require ( 'path' ) ;
6
6
const { disposableDirectory } = require ( 'disposable-directory' ) ;
7
+ const snapshot = require ( 'snapshot-assertion' ) ;
7
8
const coverageSupported = require ( '../../public/coverageSupported' ) ;
8
- const minNodeVersion = require ( '../../public/coverageSupportedMinNodeVersion' ) ;
9
9
const execFilePromise = require ( '../execFilePromise' ) ;
10
10
const stripStackTraces = require ( '../stripStackTraces' ) ;
11
11
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>' ;
13
14
14
15
module . exports = ( tests ) => {
15
16
tests . add ( '`coverage-node` CLI with 1 covered file.' , async ( ) => {
@@ -27,12 +28,24 @@ module.exports = (tests) => {
27
28
}
28
29
) ;
29
30
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
+
36
49
strictEqual ( stderr , '' ) ;
37
50
} ) ;
38
51
} ) ;
@@ -56,12 +69,24 @@ module.exports = (tests) => {
56
69
}
57
70
) ;
58
71
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
+
65
90
strictEqual ( stderr , '' ) ;
66
91
} ) ;
67
92
} ) ;
@@ -91,21 +116,35 @@ module.exports = (tests) => {
91
116
( { stdout, stderr } = error ) ;
92
117
}
93
118
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
+ )
103
146
) ;
104
- } else {
105
- strictEqual ( threw , undefined , 'CLI shouldn’t error.' ) ;
106
- strictEqual ( stdout , stdoutSkippedCodeCoverage ) ;
107
- strictEqual ( stderr , '' ) ;
108
- }
147
+ else strictEqual ( stderr , '' ) ;
109
148
} ) ;
110
149
} ) ;
111
150
@@ -162,27 +201,38 @@ require('${fileFPath}')`
162
201
( { stdout, stderr } = error ) ;
163
202
}
164
203
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 (
176
227
stderr
177
228
. replace ( relative ( '' , fileEPath ) , '<pathE>' )
178
229
. 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
+ )
180
234
) ;
181
- } else {
182
- strictEqual ( threw , undefined , 'CLI shouldn’t error.' ) ;
183
- strictEqual ( stdout , stdoutSkippedCodeCoverage ) ;
184
- strictEqual ( stderr , '' ) ;
185
- }
235
+ else strictEqual ( stderr , '' ) ;
186
236
} ) ;
187
237
}
188
238
) ;
@@ -202,12 +252,23 @@ require('${fileFPath}')`
202
252
}
203
253
) ;
204
254
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
+ ) ;
211
272
212
273
strictEqual ( stderr , '' ) ;
213
274
} ) ;
@@ -232,11 +293,15 @@ require('${fileFPath}')`
232
293
var { stdout, stderr } = error ;
233
294
}
234
295
235
- strictEqual ( threw , true , 'CLI should error.' ) ;
296
+ strictEqual ( threw , true ) ;
236
297
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' )
240
305
) ;
241
306
} ) ;
242
307
} ) ;
@@ -269,7 +334,7 @@ deprecated()`
269
334
var { stdout, stderr } = error ;
270
335
}
271
336
272
- strictEqual ( threw , true , 'CLI should error.' ) ;
337
+ strictEqual ( threw , true ) ;
273
338
strictEqual ( stdout , '' ) ;
274
339
strictEqual ( stderr . includes ( 'DeprecationWarning: Deprecated!' ) , true ) ;
275
340
} ) ;
@@ -290,13 +355,17 @@ deprecated()`
290
355
var { stdout, stderr } = error ;
291
356
}
292
357
293
- strictEqual ( threw , true , 'CLI should error.' ) ;
358
+ strictEqual ( threw , true ) ;
294
359
strictEqual ( stdout , '' ) ;
295
- strictEqual (
360
+
361
+ await snapshot (
296
362
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
+ )
300
369
) ;
301
370
} ) ;
302
371
} ;
0 commit comments