Debian Codegraph @

nyc/test/should-instrument.js

raw
1 'use strict'
2
3 const path = require('path')
4 const NYC = require('../self-coverage')
5
6 const { parseArgv } = require('./helpers')
7
8 const fixtures = path.resolve(__dirname, './fixtures')
9
10 const t = require('tap')
11
12 const rootDir = path.resolve('/')
13 t.test('should exclude appropriately with defaults', async t => {
14 const nyc = new NYC(await parseArgv(rootDir, [
15 '--exclude=test/**',
16 '--exclude=test{,-*}.js',
17 '--exclude=**/*.test.js',
18 '--exclude=**/__tests__/**'
19 ]))
20
21 // nyc always excludes "node_modules/**"
22 t.true(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo.js'), 'foo.js'))
23 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'node_modules/bar.js'), 'node_modules/bar.js'))
24 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo/node_modules/bar.js'), 'foo/node_modules/bar.js'))
25 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'test.js'), 'test.js'))
26 t.true(nyc.exclude.shouldInstrument(path.join(rootDir, 'testfoo.js'), 'testfoo.js'))
27 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'test-foo.js'), 'test-foo.js'))
28 t.true(nyc.exclude.shouldInstrument(path.join(rootDir, 'lib/test.js'), 'lib/test.js'))
29 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo/bar/test.js'), './test.js'))
30 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo/bar/test.js'), '.\\test.js'))
31 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo/bar/foo.test.js'), './foo.test.js'))
32 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo/bar/__tests__/foo.js'), './__tests__/foo.js'))
33 })
34
35 t.test('should exclude appropriately with config.exclude', async t => {
36 const nyc = new NYC(await parseArgv(fixtures))
37
38 // fixtures/package.json configures excludes: "blarg", "blerg"
39 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'blarg.js'), 'blarg.js'))
40 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'blarg/foo.js'), 'blarg/foo.js'))
41 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'blerg.js'), 'blerg.js'))
42 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'blerg.js'), './blerg.js'))
43 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'blerg.js'), '.\\blerg.js'))
44 })
45
46 t.test('should exclude outside of the current working directory', async t => {
47 const nyc = new NYC(await parseArgv(path.join(rootDir, 'foo')))
48 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'bar.js'), '../bar.js'))
49 })
50
51 t.test('should not exclude if the current working directory is inside node_modules', async t => {
52 const cwd = path.join(rootDir, 'node_modules', 'foo')
53 const nyc = new NYC(await parseArgv(cwd))
54 t.true(nyc.exclude.shouldInstrument(path.join(cwd, 'bar.js'), './bar.js'))
55 t.true(nyc.exclude.shouldInstrument(path.join(cwd, 'bar.js'), '.\\bar.js'))
56 })
57
58 t.test('allows files to be explicitly included, rather than excluded', async t => {
59 const nyc = new NYC(await parseArgv(rootDir, ['--include=foo.js']))
60
61 t.true(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo.js'), 'foo.js'))
62 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'index.js'), 'index.js'))
63 })
64
65 t.test('exclude overrides include', async t => {
66 const nyc = new NYC(await parseArgv(rootDir, [
67 '--include=foo.js',
68 '--include=test.js',
69 '--exclude=**/node_modules/**',
70 '--exclude=test/**',
71 '--exclude=test{,-*}.js'
72 ]))
73
74 t.true(nyc.exclude.shouldInstrument(path.join(rootDir, 'foo.js'), 'foo.js'))
75 t.false(nyc.exclude.shouldInstrument(path.join(rootDir, 'test.js'), 'test.js'))
76 })