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