-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
nyc-integration.js
585 lines (501 loc) · 15.8 KB
/
nyc-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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
'use strict'
const path = require('path')
const fs = require('fs')
const os = require('os')
const { promisify } = require('util')
const t = require('tap')
const glob = promisify(require('glob'))
const rimraf = promisify(require('rimraf'))
const { fixturesCLI, nycBin, runNYC, tempDirSetup, testSuccess, testFailure, envCheckConfig } = require('./helpers')
const nycConfigJS = path.resolve(fixturesCLI, 'nyc-config-js')
const nycrcDir = path.resolve(fixturesCLI, 'nycrc')
const fixturesSourceMaps = path.resolve(fixturesCLI, '../source-maps')
const fixturesENM = path.resolve(fixturesCLI, '../exclude-node-modules')
const executeNodeModulesArgs = [
'--all=true',
'--cache=false',
'--per-file=true',
'--exclude-node-modules=false',
'--include=node_modules/@istanbuljs/fake-module-1/**'
]
t.jobs = os.cpus().length
tempDirSetup(t, __filename)
t.test('--include can be used to limit bin to instrumenting specific files', t => testSuccess(t, {
args: ['--all', '--include', 'half-covered.js', process.execPath, './half-covered.js']
}))
t.test('--exclude should allow default exclude rules to be overridden', t => testSuccess(t, {
args: [
'--all',
'--exclude=**/half-covered.js',
'--exclude=**/coverage',
process.execPath,
'./half-covered.js'
]
}))
t.test('report and check should show coverage check along with report', async t => {
await testSuccess(t, {
args: ['--silent', process.execPath, './half-covered.js']
})
await testFailure(t, {
args: ['report', '--check-coverage', '--lines=100']
})
})
t.test('--ignore-class-method skips methods that match ignored name but still catches those that are not', t => testSuccess(t, {
args: ['--all', '--ignore-class-method', 'skip', process.execPath, './classes.js']
}))
t.test('--check-coverage fails when the expected coverage is below a threshold', t => testFailure(t, {
args: ['--check-coverage', '--lines', '51', process.execPath, './half-covered.js']
}))
// https://github.com/istanbuljs/nyc/issues/384
t.test('check-coverage command is equivalent to the flag', async t => {
await testSuccess(t, {
args: [process.execPath, './half-covered.js']
})
await testFailure(t, {
args: ['check-coverage', '--lines', '51']
})
})
t.test('--check-coverage succeeds when the expected coverage is above a threshold', t => testSuccess(t, {
args: ['--check-coverage', '--lines', '49', process.execPath, './half-covered.js']
}))
// https://github.com/bcoe/nyc/issues/209
t.test('--check-coverage fails in any case when the underlying test failed', t => testFailure(t, {
args: ['--check-coverage', '--lines', '49', process.execPath, './half-covered-failing.js']
}))
t.test('--check-coverage fails when the expected file coverage is below a threshold', t => testFailure(t, {
args: ['--check-coverage', '--lines', '51', '--per-file', process.execPath, './half-covered.js']
}))
t.test('passes configuration via environment variables', t => envCheckConfig(t, {
configArgs: [
'--silent',
'--require=make-dir',
'--include=env.js',
'--exclude=batman.js',
'--extension=.js',
'--cache=false',
'--cache-dir=/tmp',
'--source-map=true'
],
checkOptions: [
'instrumenter',
'silent',
'cacheDir',
'cache',
'sourceMap',
'require',
'include',
'exclude',
'extension'
]
}))
t.test('allows package.json configuration to be overridden with command line args', t => testSuccess(t, {
args: ['--reporter=text-lcov', process.execPath, './half-covered.js']
}))
t.test('loads configuration from package.json and nyc.config.js', t => testSuccess(t, {
args: [process.execPath, './index.js'],
cwd: nycConfigJS
}))
t.test('loads configuration from different module rather than nyc.config.js', t => testFailure(t, {
args: ['--all', '--nycrc-path', './nycrc-config.js', process.execPath, './index.js'],
cwd: nycConfigJS
}))
t.test('allows nyc.config.js configuration to be overridden with command line args', t => testSuccess(t, {
args: ['--all', '--exclude=foo.js', process.execPath, './index.js'],
cwd: nycConfigJS
}))
t.test('loads configuration from package.json and .nycrc', t => testSuccess(t, {
args: [process.execPath, './index.js'],
cwd: nycrcDir
}))
t.test('loads configuration from different file rather than .nycrc', t => testFailure(t, {
args: ['--nycrc-path', './.nycrc-config.json', process.execPath, './index.js'],
cwd: nycrcDir
}))
t.test('loads configuration from .nycrc.yml', t => testSuccess(t, {
args: ['--nycrc-path', './.nycrc.yml', process.execPath, './index.js'],
cwd: nycrcDir
}))
t.test('loads configuration from .nycrc.yaml', t => testSuccess(t, {
args: ['--nycrc-path', './.nycrc.yaml', process.execPath, './index.js'],
cwd: nycrcDir
}))
t.test('allows .nycrc configuration to be overridden with command line args', t => testSuccess(t, {
args: ['--exclude=foo.js', process.execPath, './index.js'],
cwd: nycrcDir
}))
t.test('reports appropriate coverage information for es6 source files', t => testSuccess(t, {
args: ['--reporter=lcov', '--reporter=text', process.execPath, './es6.js']
}))
t.test('hooks provide coverage for requireJS and AMD modules', t => testSuccess(t, {
args: [
/* This effectively excludes ./index.js, normalizing results before/after node.js 11.11.0 */
'--include=lib/**',
'--hook-run-in-this-context',
'--hook-require=false',
process.execPath,
'./index.js'
],
cwd: path.resolve(__dirname, './fixtures/hooks')
}))
t.test('does not interpret args intended for instrumented bin', async t => {
const { status, stderr, stdout } = await runNYC({
tempDir: t.tempDir,
args: ['--silent', process.execPath, 'args.js', '--help', '--version'],
leavePathSep: true
})
t.is(status, 0)
t.is(stderr, '')
t.matchSnapshot(JSON.parse(stdout).slice(2))
})
t.test('interprets first args after -- as Node.js execArgv', t => testSuccess(t, {
args: ['--', '--expose-gc', path.resolve(fixturesCLI, 'gc.js')]
}))
t.test('--show-process-tree displays a tree of spawned processes', t => testSuccess(t, {
args: ['--show-process-tree', process.execPath, 'selfspawn-fibonacci.js', '5']
}))
t.test('--set-node-options=true is functional', t => testSuccess(t, {
args: ['--set-node-options=true', process.execPath, 'selfspawn-fibonacci.js', '5']
}))
t.test('--set-node-options=false is functional', t => testSuccess(t, {
args: ['--set-node-options=false', process.execPath, 'selfspawn-fibonacci.js', '5']
}))
t.test('can run "npm test" which directly invokes a test file', t => testSuccess(t, {
args: ['npm', 'test'],
cwd: path.resolve(fixturesCLI, 'run-npm-test')
}))
t.test('can run "npm test" which indirectly invokes a test file', t => testSuccess(t, {
args: ['npm', 'test'],
cwd: path.resolve(fixturesCLI, 'run-npm-test-recursive')
}))
t.test('nyc instrument single file to console', async t => {
const { status, stderr, originalText } = await runNYC({
tempDir: t.tempDir,
args: ['instrument', './half-covered.js']
})
t.is(status, 0)
t.is(stderr, '')
t.match(originalText.stdout, `path:${JSON.stringify(path.resolve(fixturesCLI, 'half-covered.js'))}`)
})
t.test('nyc instrument a directory of files', async t => {
const { status, stderr, originalText } = await runNYC({
tempDir: t.tempDir,
args: ['instrument', './']
})
t.is(status, 0)
t.is(stderr, '')
t.match(originalText.stdout, `path:${JSON.stringify(path.resolve(fixturesCLI, 'half-covered.js'))}`)
t.match(originalText.stdout, `path:${JSON.stringify(path.resolve(fixturesCLI, 'half-covered-failing.js'))}`)
t.notMatch(originalText.stdout, `path:${JSON.stringify(path.resolve(fixturesCLI, 'test.js'))}`)
})
t.test('nyc instrument returns unmodified source if there is no transform', async t => {
const { status, stderr, stdout } = await runNYC({
tempDir: t.tempDir,
args: ['instrument', './no-transform/half-covered.xjs']
})
t.is(status, 0)
t.is(stderr, '')
t.match(stdout, 'var a = 0')
t.notMatch(stdout, 'cov_')
})
t.test('nyc instrument on file with `package` keyword when es-modules is disabled', async t => {
const { status, stderr, originalText } = await runNYC({
tempDir: t.tempDir,
args: ['instrument', '--no-es-modules', './not-strict.js']
})
t.is(status, 0)
t.is(stderr, '')
t.match(originalText.stdout, `path:${JSON.stringify(path.resolve(fixturesCLI, 'not-strict.js'))}`)
})
t.test('nyc instrument fails on file with `package` keyword when es-modules is enabled', t => testFailure(t, {
args: ['instrument', '--exit-on-error', './not-strict.js']
}))
t.test('nyc displays help to stderr when it doesn\'t know what to do', async t => {
const help = await runNYC({
tempDir: t.tempDir,
args: ['--help']
})
t.is(help.status, 0)
t.is(help.stderr, '')
t.isNot(help.stdout, '')
const { status, stderr, stdout } = await runNYC({
tempDir: t.tempDir,
args: []
})
t.equal(status, 1)
t.equal(stdout, '')
t.equal(stderr, help.stdout)
})
t.test('handles --clean / --no-clean properly', async t => {
await testSuccess(t, {
args: [
'--clean',
process.execPath,
'./by-arg2.js',
'1'
]
})
await testSuccess(t, {
args: [
'--no-clean',
process.execPath,
'./by-arg2.js',
'2'
]
})
})
t.test('setting instrument to "false" configures noop instrumenter', t => envCheckConfig(t, {
configArgs: [
'--silent',
'--no-instrument',
'--no-source-map'
],
checkOptions: [
'silent',
'instrument',
'sourceMap',
'instrumenter'
]
}))
t.test('extracts coverage headers from unexecuted files', async t => {
await envCheckConfig(t, {
configArgs: [
'--all',
'--silent',
'--no-instrument',
'--no-source-map'
],
checkOptions: [
'all',
'silent',
'instrument',
'sourceMap',
'instrumenter'
]
})
const files = await glob(path.join(t.tempDir, '*.json'))
const coverage = files.reduce(
(obj, file) => Object.assign(obj, JSON.parse(fs.readFileSync(file, 'utf-8'))),
{}
)
// we should not have executed file, so all counts sould be 0.
const data = coverage['./external-instrumenter.js']
t.type(data, 'object')
t.false(Object.values(data.s).some(s => s !== 0))
})
t.test('allows an alternative cache folder to be specified', async t => {
const cacheDir = path.resolve(fixturesCLI, 'foo-cache')
await testSuccess(t, {
args: [
`--cache-dir=${cacheDir}`,
'--cache=true',
process.execPath,
'./half-covered.js'
]
})
// we should have created foo-cache rather
// than the default ./node_modules/.cache.
t.is(1, fs.readdirSync(cacheDir).length)
await rimraf(cacheDir)
})
// see: https://github.com/istanbuljs/nyc/issues/563
t.test('does not create .cache folder if cache is "false"', async t => {
const cacheDir = path.resolve(fixturesCLI, './disabled-cache')
await testSuccess(t, {
args: [
`--cache-dir=${cacheDir}`,
'--cache=false',
process.execPath,
'./half-covered.js'
]
})
t.false(fs.existsSync(cacheDir))
})
t.test('allows alternative high and low watermarks to be configured', t => testSuccess(t, {
args: [
'--watermarks.lines=90',
'--watermarks.lines=100',
'--watermarks.statements=30',
'--watermarks.statements=40',
'--cache=true',
process.execPath,
'./half-covered.js'
],
env: {
FORCE_COLOR: true
}
}))
t.test('--all includes files with both .map files and inline source-maps', t => testSuccess(t, {
args: [
'--all',
'--cache=false',
'--exclude-after-remap=false',
'--exclude=original',
process.execPath,
'./instrumented/s1.min.js'
],
cwd: fixturesSourceMaps
}))
t.test('--all uses source-maps to exclude original sources from reports', t => testSuccess(t, {
args: [
'--all',
'--cache=false',
'--exclude=original/s1.js',
process.execPath,
'./instrumented/s1.min.js'
],
cwd: fixturesSourceMaps
}))
t.test('caches source-maps from .map files', t => {
return testSuccess(t, {
args: [
process.execPath,
'./instrumented/s1.min.js'
],
cwd: fixturesSourceMaps
}).then(() => {
const files = fs.readdirSync(path.resolve(fixturesSourceMaps, 'node_modules/.cache/nyc'))
t.true(files.some(f => f.startsWith('s1.min-') && f.endsWith('.map')))
})
})
t.test('caches inline source-maps', t => {
return testSuccess(t, {
args: [
process.execPath,
'./instrumented/s2.min.js'
],
cwd: fixturesSourceMaps
}).then(() => {
const files = fs.readdirSync(path.resolve(fixturesSourceMaps, 'node_modules/.cache/nyc'))
t.true(files.some(f => f.startsWith('s2.min-') && f.endsWith('.map')))
})
})
t.test('appropriately instruments file with corresponding .map file', t => testSuccess(t, {
args: [
'--cache=false',
'--exclude-after-remap=false',
'--exclude=original',
process.execPath,
'./instrumented/s1.min.js'
],
cwd: fixturesSourceMaps
}))
t.test('appropriately instruments file with inline source-map', t => testSuccess(t, {
args: [
'--cache=false',
'--exclude-after-remap=false',
'--exclude=original',
process.execPath,
'./instrumented/s2.min.js'
],
cwd: fixturesSourceMaps
}))
t.test('skip-empty does not display 0-line files', t => testSuccess(t, {
args: [
'--cache=false',
'--skip-empty=true',
process.execPath,
'./empty.js'
]
}))
t.test('skip-full does not display files with 100% statement, branch, and function coverage', t => testSuccess(t, {
args: [
'--skip-full',
process.execPath,
'./skip-full.js'
]
}))
t.test('allows reserved word when es-modules is disabled', t => testSuccess(t, {
args: [
'--cache=false',
'--es-modules=false',
process.execPath,
'./not-strict.js'
]
}))
t.test('forbids reserved word when es-modules is not disabled', t => testFailure(t, {
args: [
'--cache=false',
'--exit-on-error=true',
process.execPath,
'./not-strict.js'
]
}))
t.test('execute with exclude-node-modules=false', async t => {
await testFailure(t, {
args: [
...executeNodeModulesArgs,
'--check-coverage=true',
process.execPath,
'./bin/do-nothing.js'
],
cwd: fixturesENM
})
await testFailure(t, {
args: [
...executeNodeModulesArgs,
'--check-coverage=true',
'report'
],
cwd: fixturesENM
})
await testFailure(t, {
args: [
...executeNodeModulesArgs,
'check-coverage'
],
cwd: fixturesENM
})
})
t.test('instrument with exclude-node-modules=false', async t => {
const { status, stderr, stdout } = await runNYC({
tempDir: t.tempDir,
args: [
...executeNodeModulesArgs,
'instrument',
'node_modules'
],
cwd: fixturesENM
})
t.is(status, 0)
t.is(stderr, '')
t.match(stdout, 'fake-module-1')
})
t.test('recursive run does not throw', t => testSuccess(t, {
args: [
process.execPath,
nycBin,
process.execPath,
nycBin,
process.execPath,
nycBin,
'true'
],
cwd: path.resolve(__dirname, 'fixtures/recursive-run')
}))
t.test('combines multiple coverage reports', async t => {
await testSuccess(t, {
args: ['merge', './merge-input']
})
const mergedCoverage = require('./fixtures/cli/coverage')
// the combined reports should have 100% function
// branch and statement coverage.
t.strictDeepEqual(
mergedCoverage['/private/tmp/contrived/library.js'].s,
{ '0': 2, '1': 1, '2': 1, '3': 2, '4': 1, '5': 1 }
)
t.strictDeepEqual(
mergedCoverage['/private/tmp/contrived/library.js'].f,
{ '0': 1, '1': 1, '2': 2 }
)
t.strictDeepEqual(
mergedCoverage['/private/tmp/contrived/library.js'].b,
{ '0': [1, 1] }
)
await rimraf(path.resolve(fixturesCLI, 'coverage.json'))
})
t.test('reports error if input directory is missing', t => testFailure(t, {
args: ['merge', './DIRECTORY_THAT_IS_MISSING']
}))
t.test('reports error if input is not a directory', t => testFailure(t, {
args: ['merge', './package.json']
}))