/
integration.js
522 lines (486 loc) · 16.5 KB
/
integration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/* global describe, before, beforeEach, it */
const { readFileSync } = require('fs')
const { resolve } = require('path')
const { spawnSync } = require('child_process')
const { statSync } = require('fs')
const { dirname } = require('path')
const c8Path = require.resolve('../bin/c8')
const nodePath = process.execPath
const tsNodePath = './node_modules/.bin/ts-node'
const chaiJestSnapshot = require('chai-jest-snapshot')
const rimraf = require('rimraf')
require('chai')
.use(chaiJestSnapshot)
.should()
before(cb => rimraf('tmp', cb))
const nodeMajorVersion = Number(process.version.slice(1).split('.')[0])
beforeEach(function () {
// Node 10 is missing some of the patches to V8 that improve coverage in
// newer Node.js versions, for this reason it requires its own snapshot file.
if (nodeMajorVersion === 10) {
const file = this.currentTest.file
this.currentTest.file = `${file}_10`
}
chaiJestSnapshot.configureUsingMochaContext(this)
})
describe('c8', () => {
it('reports coverage for script that exits normally', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/normal',
'--clean=false',
nodePath,
require.resolve('./fixtures/normal')
])
output.toString('utf8').should.matchSnapshot()
})
it('supports exeternally set NODE_V8_COVERAGE', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--clean=false',
nodePath,
require.resolve('./fixtures/normal')
], {
env: {
NODE_V8_COVERAGE: 'tmp/override'
}
})
const stats = statSync('tmp/override')
stats.isDirectory().should.equal(true)
output.toString('utf8').should.matchSnapshot()
})
it('merges reports from subprocesses together', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/multiple-spawn',
'--clean=false',
nodePath,
require.resolve('./fixtures/multiple-spawn')
])
output.toString('utf8').should.matchSnapshot()
})
it('allows relative files to be included', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/multiple-spawn-2',
'--omit-relative=false',
'--clean=false',
nodePath,
require.resolve('./fixtures/multiple-spawn')
])
output.toString('utf8').should.match(
/Error: ENOENT: no such file or directory.*loaders\.js/
)
})
it('exits with 1 when report output fails', () => {
const { status, stderr } = spawnSync(nodePath, [
c8Path,
'--clean=false',
'--reporter=unknown',
nodePath,
'--version'
])
status.should.equal(1)
stderr.toString().should.match(/Cannot find module 'unknown'/u)
})
it('should allow for files outside of cwd', () => {
// Here we nest this test into the report directory making the multidir
// directories outside of cwd. If the `--allowExternal` flag is not provided
// the multidir files will now show up in the file report, even though they were
// required in
const { output, status } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/allowExternal',
'--clean=true',
'--allowExternal',
'--reporter=text',
nodePath,
require.resolve('./fixtures/report/allowExternal.js')
], {
cwd: dirname(require.resolve('./fixtures/report/allowExternal.js'))
})
status.should.equal(0)
output.toString('utf8').should.matchSnapshot()
})
it('should allow for multiple overrides of src location for --all', () => {
// Here we nest this test into the report directory making the multidir
// directories outside of cwd. Note, that the target srcOverride does not
// require fiels from these directories but we want them initialized to 0
// via --all. As such we --allowExternal and provide multiple --src patterns
// to override cwd.
const { output, status } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=../tmp/src',
'--clean=true',
'--allowExternal',
'--reporter=text',
'--all',
`--src=${dirname(require.resolve('./fixtures/multidir1/file1.js'))}`,
`--src=${dirname(require.resolve('./fixtures/multidir2/file2.js'))}`,
`--src=${dirname(require.resolve('./fixtures/report/srcOverride.js'))}`,
nodePath,
require.resolve('./fixtures/report/srcOverride.js')
], {
cwd: dirname(require.resolve('./fixtures/report/srcOverride.js'))
})
status.should.equal(0)
output.toString('utf8').should.matchSnapshot()
})
describe('check-coverage', () => {
before(() => {
spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/check-coverage',
'--clean=false',
nodePath,
require.resolve('./fixtures/normal')
])
})
it('exits with 0 if coverage within threshold', () => {
const { output, status } = spawnSync(nodePath, [
c8Path,
'check-coverage',
'--exclude="test/fixtures/*.js"',
'--temp-directory=tmp/check-coverage',
'--lines=70',
'--branches=56',
'--statements=70'
])
status.should.equal(0)
output.toString('utf8').should.matchSnapshot()
})
it('exits with 1 if coverage is below threshold', () => {
const { output, status } = spawnSync(nodePath, [
c8Path,
'check-coverage',
'--exclude="test/*.js"',
'--temp-directory=tmp/check-coverage',
'--lines=101'
])
status.should.equal(1)
output.toString('utf8').should.matchSnapshot()
})
it('allows threshold to be applied on per-file basis', () => {
const { output, status } = spawnSync(nodePath, [
c8Path,
'check-coverage',
'--exclude="test/*.js"',
'--temp-directory=tmp/check-coverage',
'--lines=101',
'--per-file'
])
status.should.equal(1)
output.toString('utf8').should.matchSnapshot()
})
it('allows --check-coverage when executing script', () => {
const { output, status } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--clean=false',
'--temp-directory=tmp/check-coverage',
'--lines=101',
'--check-coverage',
nodePath,
require.resolve('./fixtures/normal')
])
status.should.equal(1)
output.toString('utf8').should.matchSnapshot()
})
})
describe('report', () => {
before(() => {
spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=./tmp/report',
'--clean=false',
nodePath,
require.resolve('./fixtures/normal')
])
})
it('generates report from existing temporary files', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'report',
'--exclude="test/*.js"',
'--temp-directory=./tmp/report',
'--clean=false'
])
output.toString('utf8').should.matchSnapshot()
})
it('supports --check-coverage, when generating reports', () => {
const { output, status } = spawnSync(nodePath, [
c8Path,
'report',
'--check-coverage',
'--lines=101',
'--exclude="test/*.js"',
'--temp-directory=tmp/report',
'--clean=false'
])
status.should.equal(1)
output.toString('utf8').should.matchSnapshot()
})
})
describe('ESM Modules', () => {
it('collects coverage for ESM modules', () => {
if (nodeMajorVersion === 10) return
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--clean=false',
'--temp-directory=tmp/esm',
nodePath,
'--experimental-modules',
'--no-warnings',
require.resolve('./fixtures/import.mjs')
])
output.toString('utf8').should.matchSnapshot()
})
})
describe('/* c8 ignore next */', () => {
it('ignores lines with special comment', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--clean=false',
'--temp-directory=tmp/normal',
nodePath,
require.resolve('./fixtures/c8-ignore-next.js')
])
output.toString('utf8').should.matchSnapshot()
})
})
describe('source-maps', () => {
beforeEach(cb => rimraf('tmp/source-map', cb))
describe('TypeScript', () => {
// Bugs:
// closing '}' on `if` is not covered.
it('remaps branches', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/source-map',
'--clean=true',
nodePath,
require.resolve('./fixtures/source-maps/branches/branches.typescript.js')
])
output.toString('utf8').should.matchSnapshot()
})
// Bugs:
// closing '}' on `if` is not covered.
it('remaps classes', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/source-map',
'--clean=true',
nodePath,
require.resolve('./fixtures/source-maps/classes/classes.typescript.js')
])
output.toString('utf8').should.matchSnapshot()
})
})
describe('UglifyJS', () => {
// Bugs:
// string in `console.info` shown as uncovered branch.
it('remaps branches', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/source-map',
'--clean=true',
nodePath,
require.resolve('./fixtures/source-maps/branches/branches.uglify.js')
])
output.toString('utf8').should.matchSnapshot()
})
// Bugs:
// string in `console.info` shown as uncovered branch.
it('remaps classes', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/source-map',
'--clean=true',
nodePath,
require.resolve('./fixtures/source-maps/classes/classes.uglify.js')
])
output.toString('utf8').should.matchSnapshot()
})
})
describe('nyc', () => {
// Bugs:
// first 'if' statement indicates two odd missing branches.
// line 4 should be covered.
it('remaps branches', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/source-map',
'--clean=true',
nodePath,
require.resolve('./fixtures/source-maps/branches/branches.nyc.js')
])
output.toString('utf8').should.matchSnapshot()
})
// Bugs:
// the portion `class F` of `class Foo` indicates missing branch.
// line 6 should be covered.
it('remaps classes', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/source-map',
'--clean=true',
nodePath,
require.resolve('./fixtures/source-maps/classes/classes.nyc.js')
])
output.toString('utf8').should.matchSnapshot()
})
})
})
describe('ts-node', () => {
beforeEach(cb => rimraf('tmp/source-map', cb))
it('reads source-map from cache, and applies to coverage', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/source-map',
'--clean=true',
tsNodePath,
require.resolve('./fixtures/ts-node-basic.ts')
])
output.toString('utf8').should.matchSnapshot()
})
})
describe('--all', () => {
it('reports coverage for unloaded js files as 0 for line, branch and function', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--temp-directory=tmp/vanilla-all',
'--clean=false',
'--all=true',
'--include=test/fixtures/all/vanilla/**/*.js',
'--exclude=**/*.ts', // add an exclude to avoid default excludes of test/**
nodePath,
require.resolve('./fixtures/all/vanilla/main')
])
output.toString('utf8').should.matchSnapshot()
})
it('reports coverage for unloaded transpiled ts files as 0 for line, branch and function', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--temp-directory=tmp/all-ts',
'--clean=false',
'--all=true',
'--include=test/fixtures/all/ts-compiled/**/*.js',
'--exclude="test/*.js"', // add an exclude to avoid default excludes of test/**
nodePath,
require.resolve('./fixtures/all/ts-compiled/main.js')
])
output.toString('utf8').should.matchSnapshot()
})
it('reports coverage for unloaded ts files as 0 for line, branch and function when using ts-node', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--temp-directory=tmp/all-ts-node',
'--clean=false',
'--all=true',
'--include=test/fixtures/all/ts-only/**/*.ts',
'--exclude="test/*.js"', // add an exclude to avoid default excludes of test/**
tsNodePath,
require.resolve('./fixtures/all/ts-only/main.ts')
])
output.toString('utf8').should.matchSnapshot()
})
it('should allow for --all to be used in conjunction with --check-coverage', () => {
const { output } = spawnSync(nodePath, [
c8Path,
'--temp-directory=tmp/all-check-coverage',
'--clean=false',
'--check-coverage',
'--lines=100',
'--all=true',
'--include=test/fixtures/all/vanilla/**/*.js',
'--exclude=**/*.ts', // add an exclude to avoid default excludes of test/**
nodePath,
require.resolve('./fixtures/all/vanilla/main')
])
output.toString('utf8').should.matchSnapshot()
})
it('should allow for --all to be used with the check-coverage command (2 invocations)', () => {
// generate v8 output
spawnSync(nodePath, [
c8Path,
'--temp-directory=tmp/all-check-coverage-as-command',
'--clean=false',
'--check-coverage',
'--lines=90',
'--all=true',
'--include=test/fixtures/all/vanilla/**/*.js',
'--exclude=**/*.ts', // add an exclude to avoid default excludes of test/**
nodePath,
require.resolve('./fixtures/all/vanilla/main')
])
// invoke check-coverage as a command with --all
const { output } = spawnSync(nodePath, [
c8Path,
'check-coverage',
'--lines=90',
'--temp-directory=tmp/all-check-coverage-as-command',
'--clean=false',
'--all=true',
'--include=test/fixtures/all/vanilla/**/*.js',
'--exclude=**/*.ts' // add an exclude to avoid default excludes of test/**
])
output.toString('utf8').should.matchSnapshot()
})
})
// see: https://github.com/bcoe/c8/issues/149
it('cobertura report escapes special characters', () => {
spawnSync(nodePath, [
c8Path,
'--exclude="test/*.js"',
'--temp-directory=tmp/cobertura',
'--clean=false',
'--reporter=cobertura',
nodePath,
require.resolve('./fixtures/computed-method')
])
const cobertura = readFileSync(resolve(process.cwd(), './coverage/cobertura-coverage.xml'), 'utf8')
.replace(/[0-9]{13,}/, 'nnnn')
.replace(/<source>.*<\/source>/, '<source>/foo/file</source>')
.replace(/\\/g, '/')
cobertura.toString('utf8').should.matchSnapshot()
})
describe('report', () => {
it('supports reporting on directories outside cwd', () => {
// invoke a script that uses report as an api and supplies src dirs out
// of cwd
const { output } = spawnSync(nodePath, [
require.resolve('./fixtures/report/report-multi-dir-external.js')
], {
cwd: dirname(require.resolve('./fixtures/report/report-multi-dir-external.js'))
})
output.toString('utf8').should.matchSnapshot()
})
it('supports reporting on single directories outside cwd', () => {
// invoke a script that uses report as an api and supplies src dirs out
// of cwd.
const { output } = spawnSync(nodePath, [
require.resolve('./fixtures/report/report-single-dir-external.js')
], {
cwd: dirname(require.resolve('./fixtures/report/report-single-dir-external.js'))
})
output.toString('utf8').should.matchSnapshot()
})
})
})