Debian Codegraph @

nyc/test/nyc-integration.js

raw ◀ ▶
2 Debian patches touch this file
1 'use strict'
2
3 const path = require('path')
4 const fs = require('../lib/fs-promises')
5 const os = require('os')
6 const { promisify } = require('util')
7
8 const t = require('tap')
•9 const { glob } = require('glob')
•10 const { rimraf } = require('rimraf')
11
12 const { fixturesCLI, nycBin, runNYC, tempDirSetup, testSuccess, testFailure, envCheckConfig } = require('./helpers')
13
14 const nycConfigJS = path.resolve(fixturesCLI, 'nyc-config-js')
15 const nycrcDir = path.resolve(fixturesCLI, 'nycrc')
16 const fixturesSourceMaps = path.resolve(fixturesCLI, '../source-maps')
17 const fixturesENM = path.resolve(fixturesCLI, '../exclude-node-modules')
18 const fixturesAllTypeModule = path.resolve(fixturesCLI, '../all-type-module')
19
20 const executeNodeModulesArgs = [
21 '--all=true',
22 '--cache=false',
23 '--per-file=true',
24 '--exclude-node-modules=false',
25 '--include=node_modules/@istanbuljs/fake-module-1/**'
26 ]
27
28 t.jobs = os.cpus().length
29
30 tempDirSetup(t, __filename)
31
32 t.test('--include can be used to limit bin to instrumenting specific files', t => testSuccess(t, {
33 args: ['--all', '--include', 'half-covered.js', process.execPath, './half-covered.js']
34 }))
35
36 t.test('--exclude should allow default exclude rules to be overridden', t => testSuccess(t, {
37 args: [
38 '--all',
39 '--exclude=**/half-covered.js',
40 '--exclude=**/coverage',
41 process.execPath,
42 './half-covered.js'
43 ]
44 }))
45
46 t.test('report and check should show coverage check along with report', async t => {
47 await testSuccess(t, {
48 args: ['--silent', process.execPath, './half-covered.js']
49 })
50 await testFailure(t, {
51 args: ['report', '--check-coverage', '--lines=100']
52 })
53 })
54
55 t.test('--ignore-class-method skips methods that match ignored name but still catches those that are not', t => testSuccess(t, {
56 args: ['--all', '--ignore-class-method', 'skip', process.execPath, './classes.js']
57 }))
58
59 t.test('--check-coverage fails when the expected coverage is below a threshold', t => testFailure(t, {
60 args: ['--check-coverage', '--lines', '51', process.execPath, './half-covered.js']
61 }))
62
63 // https://github.com/istanbuljs/nyc/issues/384
64 t.test('check-coverage command is equivalent to the flag', async t => {
65 await testSuccess(t, {
66 args: [process.execPath, './half-covered.js']
67 })
68
69 await testFailure(t, {
70 args: ['check-coverage', '--lines', '51']
71 })
72 })
73
74 t.test('--check-coverage succeeds when the expected coverage is above a threshold', t => testSuccess(t, {
75 args: ['--check-coverage', '--lines', '49', process.execPath, './half-covered.js']
76 }))
77
78 // https://github.com/bcoe/nyc/issues/209
79 t.test('--check-coverage fails in any case when the underlying test failed', t => testFailure(t, {
80 args: ['--check-coverage', '--lines', '49', process.execPath, './half-covered-failing.js']
81 }))
82
83 t.test('--check-coverage fails when the expected file coverage is below a threshold', t => testFailure(t, {
84 args: ['--check-coverage', '--lines', '51', '--per-file', process.execPath, './half-covered.js']
85 }))
86
87 t.test('passes configuration via environment variables', t => envCheckConfig(t, {
88 configArgs: [
89 '--silent',
90 '--require=make-dir',
91 '--include=env.js',
92 '--exclude=batman.js',
93 '--extension=.js',
94 '--cache=false',
95 '--cache-dir=/tmp',
96 '--source-map=true'
97 ],
98 checkOptions: [
99 'instrumenter',
100 'silent',
101 'cacheDir',
102 'cache',
103 'sourceMap',
104 'require',
105 'include',
106 'exclude',
107 'extension'
108 ]
109 }))
110
111 t.test('allows package.json configuration to be overridden with command line args', t => testSuccess(t, {
112 args: ['--reporter=text-lcov', process.execPath, './half-covered.js']
113 }))
114
115 t.test('loads configuration from package.json and nyc.config.js', t => testSuccess(t, {
116 args: [process.execPath, './index.js'],
117 cwd: nycConfigJS
118 }))
119
120 t.test('loads configuration from different module rather than nyc.config.js', t => testFailure(t, {
121 args: ['--all', '--nycrc-path', './nycrc-config.js', process.execPath, './index.js'],
122 cwd: nycConfigJS
123 }))
124
125 t.test('allows nyc.config.js configuration to be overridden with command line args', t => testSuccess(t, {
126 args: ['--all', '--exclude=foo.js', process.execPath, './index.js'],
127 cwd: nycConfigJS
128 }))
129
130 t.test('loads configuration from package.json and .nycrc', t => testSuccess(t, {
131 args: [process.execPath, './index.js'],
132 cwd: nycrcDir
133 }))
134
135 t.test('loads configuration from different file rather than .nycrc', t => testFailure(t, {
136 args: ['--nycrc-path', './.nycrc-config.json', process.execPath, './index.js'],
137 cwd: nycrcDir
138 }))
139
140 t.test('loads configuration from .nycrc.yml', t => testSuccess(t, {
141 args: ['--nycrc-path', './.nycrc.yml', process.execPath, './index.js'],
142 cwd: nycrcDir
143 }))
144
145 t.test('loads configuration from .nycrc.yaml', t => testSuccess(t, {
146 args: ['--nycrc-path', './.nycrc.yaml', process.execPath, './index.js'],
147 cwd: nycrcDir
148 }))
149
150 t.test('allows .nycrc configuration to be overridden with command line args', t => testSuccess(t, {
151 args: ['--exclude=foo.js', process.execPath, './index.js'],
152 cwd: nycrcDir
153 }))
154
155 t.test('reports appropriate coverage information for es6 source files', t => testSuccess(t, {
156 args: ['--reporter=lcov', '--reporter=text', process.execPath, './es6.js']
157 }))
158
159 t.test('hooks provide coverage for requireJS and AMD modules', t => testSuccess(t, {
160 args: [
161 /* This effectively excludes ./index.js, normalizing results before/after node.js 11.11.0 */
162 '--include=lib/**',
163 '--hook-run-in-this-context',
164 '--hook-require=false',
165 process.execPath,
166 './index.js'
167 ],
168 cwd: path.resolve(__dirname, './fixtures/hooks')
169 }))
170
171 t.test('run-in-context provide coverage for vm.runInContext', t => testSuccess(t, {
172 args: [
173 '--hook-run-in-context',
174 '--hook-require=false',
175 process.execPath,
176 './run-in-context.js'
177 ],
178 cwd: path.resolve(__dirname, './fixtures/hooks')
179 }))
180
181 t.test('does not interpret args intended for instrumented bin', async t => {
182 const { status, stderr, stdout } = await runNYC({
183 tempDir: t.tempDir,
184 args: ['--silent', process.execPath, 'args.js', '--help', '--version'],
185 leavePathSep: true
186 })
187 t.is(status, 0)
188 t.is(stderr, '')
189 t.matchSnapshot(JSON.parse(stdout).slice(2))
190 })
191
192 t.test('interprets first args after -- as Node.js execArgv', t => testSuccess(t, {
193 args: ['--', '--expose-gc', path.resolve(fixturesCLI, 'gc.js')]
194 }))
195
196 t.test('--show-process-tree displays a tree of spawned processes', t => testSuccess(t, {
197 args: ['--show-process-tree', process.execPath, 'selfspawn-fibonacci.js', '5']
198 }))
199
200 t.test('--use-spawn-wrap=true is functional', t => testSuccess(t, {
201 args: ['--use-spawn-wrap=true', process.execPath, 'selfspawn-fibonacci.js', '5']
202 }))
203
204 t.test('--use-spawn-wrap=false is functional', t => testSuccess(t, {
205 args: ['--use-spawn-wrap=false', process.execPath, 'selfspawn-fibonacci.js', '5']
206 }))
207
208 t.test('can run "npm test" which directly invokes a test file', t => testSuccess(t, {
209 args: ['npm', 'test'],
210 cwd: path.resolve(fixturesCLI, 'run-npm-test')
211 }))
212
213 t.test('can run "npm test" which indirectly invokes a test file', t => testSuccess(t, {
214 args: ['npm', 'test'],
215 cwd: path.resolve(fixturesCLI, 'run-npm-test-recursive')
216 }))
217
218 t.test('nyc instrument single file to console', async t => {
219 const { status, stderr, originalText } = await runNYC({
220 tempDir: t.tempDir,
221 args: ['instrument', './half-covered.js']
222 })
223
224 t.is(status, 0)
225 t.is(stderr, '')
226 t.match(originalText.stdout, `path:${JSON.stringify(path.resolve(fixturesCLI, 'half-covered.js'))}`)
227 })
228
229 t.test('nyc instrument disabled instrument', async t => {
230 const { status, stderr, stdout } = await runNYC({
231 tempDir: t.tempDir,
232 args: ['instrument', '--instrument=false', 'half-covered.js']
233 })
234
235 t.is(status, 0)
236 t.is(stderr, '')
237 t.match(stdout, 'var a = 0')
238 t.notMatch(stdout, 'cov_')
239 })
240
241 t.test('nyc instrument a directory of files', async t => {
242 const { status, stderr, originalText } = await runNYC({
243 tempDir: t.tempDir,
244 args: ['instrument', './']
245 })
246
247 t.is(status, 0)
248 t.is(stderr, '')
249 t.match(originalText.stdout, `path:${JSON.stringify(path.resolve(fixturesCLI, 'half-covered.js'))}`)
250 t.match(originalText.stdout, `path:${JSON.stringify(path.resolve(fixturesCLI, 'half-covered-failing.js'))}`)
251 t.notMatch(originalText.stdout, `path:${JSON.stringify(path.resolve(fixturesCLI, 'test.js'))}`)
252 })
253
254 t.test('nyc instrument returns unmodified source if there is no transform', async t => {
255 const { status, stderr, stdout } = await runNYC({
256 tempDir: t.tempDir,
257 args: ['instrument', './no-transform/half-covered.xjs']
258 })
259
260 t.is(status, 0)
261 t.is(stderr, '')
262 t.match(stdout, 'var a = 0')
263 t.notMatch(stdout, 'cov_')
264 })
265
266 t.test('nyc instrument on file with `package` keyword when es-modules is disabled', async t => {
267 const { status, stderr, originalText } = await runNYC({
268 tempDir: t.tempDir,
269 args: ['instrument', '--no-es-modules', './not-strict.js']
270 })
271
272 t.is(status, 0)
273 t.is(stderr, '')
274 t.match(originalText.stdout, `path:${JSON.stringify(path.resolve(fixturesCLI, 'not-strict.js'))}`)
275 })
276
277 t.test('nyc instrument fails on file with `package` keyword when es-modules is enabled', t => testFailure(t, {
278 args: ['instrument', '--exit-on-error', './not-strict.js']
279 }))
280
281 t.test('nyc displays help to stderr when it doesn\'t know what to do', async t => {
282 const help = await runNYC({
283 tempDir: t.tempDir,
284 args: ['--help']
285 })
286
287 t.is(help.status, 0)
288 t.is(help.stderr, '')
289 t.isNot(help.stdout, '')
290
291 const { status, stderr, stdout } = await runNYC({
292 tempDir: t.tempDir,
293 args: []
294 })
295
296 t.equal(status, 1)
297 t.equal(stdout, '')
298 t.match(stderr, help.stdout)
299 })
300
301 t.test('handles --clean / --no-clean properly', async t => {
302 await testSuccess(t, {
303 args: [
304 '--clean',
305 process.execPath,
306 './by-arg2.js',
307 '1'
308 ]
309 })
310
311 await testSuccess(t, {
312 args: [
313 '--no-clean',
314 process.execPath,
315 './by-arg2.js',
316 '2'
317 ]
318 })
319 })
320
321 t.test('setting instrument to "false" configures noop instrumenter', t => envCheckConfig(t, {
322 configArgs: [
323 '--silent',
324 '--no-instrument',
325 '--no-source-map'
326 ],
327 checkOptions: [
328 'silent',
329 'instrument',
330 'sourceMap',
331 'instrumenter'
332 ]
333 }))
334
335 t.test('extracts coverage headers from unexecuted files', async t => {
336 await envCheckConfig(t, {
337 configArgs: [
338 '--all',
339 '--silent',
340 '--no-instrument',
341 '--no-source-map'
342 ],
343 checkOptions: [
344 'all',
345 'silent',
346 'instrument',
347 'sourceMap',
348 'instrumenter'
349 ]
350 })
351
352 const files = await glob(path.join(t.tempDir, '*.json'))
353 const coverage = []
354 await Promise.all(files.map(async file => {
355 const data = JSON.parse(await fs.readFile(file, 'utf-8'))
356 if (data['./external-instrumenter.js']) {
357 coverage.push(data['./external-instrumenter.js'])
358 }
359 }))
360
361 t.true(coverage.length !== 0)
362 t.true(coverage.every(data => typeof data === 'object'))
363 // we should not have executed file, so all counts sould be 0.
364 t.true(coverage.every(data => Object.values(data.s).every(s => s === 0)))
365 })
366
367 t.test('allows an alternative cache folder to be specified', async t => {
368 const cacheDir = path.resolve(fixturesCLI, 'foo-cache')
369
370 await testSuccess(t, {
371 args: [
372 `--cache-dir=${cacheDir}`,
373 '--cache=true',
374 process.execPath,
375 './half-covered.js'
376 ]
377 })
378
379 // we should have created foo-cache rather
380 // than the default ./node_modules/.cache.
381 t.is(1, (await fs.readdir(cacheDir)).length)
382
383 await rimraf(cacheDir)
384 })
385
386 // see: https://github.com/istanbuljs/nyc/issues/563
387 t.test('does not create .cache folder if cache is "false"', async t => {
388 const cacheDir = path.resolve(fixturesCLI, './disabled-cache')
389
390 await testSuccess(t, {
391 args: [
392 `--cache-dir=${cacheDir}`,
393 '--cache=false',
394 process.execPath,
395 './half-covered.js'
396 ]
397 })
398
399 t.false(fs.existsSync(cacheDir))
400 })
401
402 t.test('allows alternative high and low watermarks to be configured', t => testSuccess(t, {
403 args: [
404 '--watermarks.lines=90',
405 '--watermarks.lines=100',
406 '--watermarks.statements=30',
407 '--watermarks.statements=40',
408 '--cache=true',
409 process.execPath,
410 './half-covered.js'
411 ],
412 env: {
413 FORCE_COLOR: true
414 }
415 }))
416
417 t.test('--all includes files with both .map files and inline source-maps', t => testSuccess(t, {
418 args: [
419 '--all',
420 '--cache=false',
421 '--exclude-after-remap=false',
422 '--exclude=original',
423 process.execPath,
424 './instrumented/s1.min.js'
425 ],
426 cwd: fixturesSourceMaps
427 }))
428
429 t.test('--all uses source-maps to exclude original sources from reports', t => testSuccess(t, {
430 args: [
431 '--all',
432 '--cache=false',
433 '--exclude=original/s1.js',
434 process.execPath,
435 './instrumented/s1.min.js'
436 ],
437 cwd: fixturesSourceMaps
438 }))
439
440 t.test('--all does not fail on ERR_REQUIRE_ESM', t => testSuccess(t, {
441 args: [
442 '--all',
443 process.execPath,
444 'script.cjs'
445 ],
446 cwd: fixturesAllTypeModule
447 }))
448
449 t.test('caches source-maps from .map files', async t => {
450 await testSuccess(t, {
451 args: [
452 process.execPath,
453 './instrumented/s1.min.js'
454 ],
455 cwd: fixturesSourceMaps
456 })
457
458 const files = await fs.readdir(path.resolve(fixturesSourceMaps, 'node_modules/.cache/nyc'))
459 t.true(files.some(f => f.startsWith('s1.min-') && f.endsWith('.map')))
460 })
461
462 t.test('caches inline source-maps', async t => {
463 await testSuccess(t, {
464 args: [
465 process.execPath,
466 './instrumented/s2.min.js'
467 ],
468 cwd: fixturesSourceMaps
469 })
470
471 const files = await fs.readdir(path.resolve(fixturesSourceMaps, 'node_modules/.cache/nyc'))
472 t.true(files.some(f => f.startsWith('s2.min-') && f.endsWith('.map')))
473 })
474
475 t.test('appropriately instruments file with corresponding .map file', t => testSuccess(t, {
476 args: [
477 '--cache=false',
478 '--exclude-after-remap=false',
479 '--exclude=original',
480 process.execPath,
481 './instrumented/s1.min.js'
482 ],
483 cwd: fixturesSourceMaps
484 }))
485
486 t.test('appropriately instruments file with inline source-map', t => testSuccess(t, {
487 args: [
488 '--cache=false',
489 '--exclude-after-remap=false',
490 '--exclude=original',
491 process.execPath,
492 './instrumented/s2.min.js'
493 ],
494 cwd: fixturesSourceMaps
495 }))
496
497 t.test('skip-empty does not display 0-line files', t => testSuccess(t, {
498 args: [
499 '--cache=false',
500 '--skip-empty=true',
501 process.execPath,
502 './empty.js'
503 ]
504 }))
505
506 t.test('skip-full does not display files with 100% statement, branch, and function coverage', t => testSuccess(t, {
507 args: [
508 '--skip-full',
509 process.execPath,
510 './skip-full.js'
511 ]
512 }))
513
514 t.test('allows reserved word when es-modules is disabled', t => testSuccess(t, {
515 args: [
516 '--cache=false',
517 '--es-modules=false',
518 process.execPath,
519 './not-strict.js'
520 ]
521 }))
522
523 t.test('forbids reserved word when es-modules is not disabled', t => testFailure(t, {
524 args: [
525 '--cache=false',
526 '--exit-on-error=true',
527 process.execPath,
528 './not-strict.js'
529 ]
530 }))
531
532 t.test('execute with exclude-node-modules=false', async t => {
533 await testFailure(t, {
534 args: [
535 ...executeNodeModulesArgs,
536 '--check-coverage=true',
537 process.execPath,
538 './bin/do-nothing.js'
539 ],
540 cwd: fixturesENM
541 })
542
543 await testFailure(t, {
544 args: [
545 ...executeNodeModulesArgs,
546 '--check-coverage=true',
547 'report'
548 ],
549 cwd: fixturesENM
550 })
551
552 await testSuccess(t, {
553 args: [
554 ...executeNodeModulesArgs,
555 '--check-coverage=false',
556 'report'
557 ],
558 cwd: fixturesENM
559 })
560
561 await testFailure(t, {
562 args: [
563 ...executeNodeModulesArgs,
564 'check-coverage'
565 ],
566 cwd: fixturesENM
567 })
568 })
569
570 t.test('instrument with exclude-node-modules=false', async t => {
571 const { status, stderr, stdout } = await runNYC({
572 tempDir: t.tempDir,
573 args: [
574 ...executeNodeModulesArgs,
575 'instrument',
576 'node_modules'
577 ],
578 cwd: fixturesENM
579 })
580
581 t.is(status, 0)
582 t.is(stderr, '')
583 t.match(stdout, 'fake-module-1')
584 })
585
586 t.test('recursive run does not throw', t => testSuccess(t, {
587 args: [
588 process.execPath,
589 nycBin,
590 process.execPath,
591 nycBin,
592 process.execPath,
593 nycBin,
594 'true'
595 ],
596 cwd: path.resolve(__dirname, 'fixtures/recursive-run')
597 }))
598
599 t.test('combines multiple coverage reports', async t => {
600 await testSuccess(t, {
601 args: ['merge', './merge-input']
602 })
603
604 const mergedCoverage = require('./fixtures/cli/coverage')
605 // the combined reports should have 100% function
606 // branch and statement coverage.
607 t.strictDeepEqual(
608 mergedCoverage['/private/tmp/contrived/library.js'].s,
609 { 0: 2, 1: 1, 2: 1, 3: 2, 4: 1, 5: 1 }
610 )
611 t.strictDeepEqual(
612 mergedCoverage['/private/tmp/contrived/library.js'].f,
613 { 0: 1, 1: 1, 2: 2 }
614 )
615 t.strictDeepEqual(
616 mergedCoverage['/private/tmp/contrived/library.js'].b,
617 { 0: [1, 1] }
618 )
619 await rimraf(path.resolve(fixturesCLI, 'coverage.json'))
620 })
621
622 t.test('reports error if input directory is missing', t => testFailure(t, {
623 args: ['merge', './DIRECTORY_THAT_IS_MISSING']
624 }))
625
626 t.test('reports error if input is not a directory', t => testFailure(t, {
627 args: ['merge', './package.json']
628 }))
629
630 t.test('--all instruments unknown extensions as js', t => testSuccess(t, {
631 cwd: path.resolve(fixturesCLI, '../conf-multiple-extensions'),
632 args: ['--all', process.execPath, './run.js']
633 }))
634
635 t.test('instrument with invalid --require fails when using node-preload', async t => {
636 const { status, stderr, stdout } = await runNYC({
637 tempDir: t.tempDir,
638 args: [
639 '--require=@istanbuljs/this-module-does-not-exist',
640 'instrument',
641 './skip-full.js'
642 ]
643 })
644
645 t.is(status, 1)
646 t.match(stderr, /Cannot find module '@istanbuljs\/this-module-does-not-exist'/)
647 t.is(stdout, '')
648 })
649
650 t.test('invalid --require fails when using node-preload', async t => {
651 const { status, stderr, stdout } = await runNYC({
652 tempDir: t.tempDir,
653 args: [
654 '--require=@istanbuljs/this-module-does-not-exist',
655 './skip-full.js'
656 ]
657 })
658
659 t.is(status, 1)
660 t.match(stderr, /Cannot find module '@istanbuljs\/this-module-does-not-exist'/)
661 t.is(stdout, '')
662 })
663
664 t.test('invalid --require fails when using spawn-wrap', async t => {
665 const { status, stderr, stdout } = await runNYC({
666 tempDir: t.tempDir,
667 args: [
668 '--use-spawn-wrap=true',
669 '--require=@istanbuljs/this-module-does-not-exist',
670 'instrument',
671 './skip-full.js'
672 ]
673 })
674
675 t.is(status, 1)
676 t.match(stderr, /Cannot find module '@istanbuljs\/this-module-does-not-exist'/)
677 t.is(stdout, '')
678 })