| 1 |
'use strict' |
| 2 |
|
| 3 |
const fs = require('../lib/fs-promises') |
| 4 |
const path = require('path') |
| 5 |
|
| 6 |
const t = require('tap') |
| 7 |
const ap = require('any-path') |
| 8 |
|
| 9 |
const NYC = require('../self-coverage') |
| 10 |
|
| 11 |
const { parseArgv, resetState } = require('./helpers') |
| 12 |
|
| 13 |
const fixtures = path.resolve(__dirname, 'fixtures') |
| 14 |
const transpileHook = path.resolve(__dirname, 'fixtures/transpile-hook') |
| 15 |
|
| 16 |
t.beforeEach(resetState) |
| 17 |
|
| 18 |
t.test('outputs an empty coverage report for all files that are not excluded', async t => { |
| 19 |
const nyc = new NYC(await parseArgv(fixtures)) |
| 20 |
await nyc.reset() |
| 21 |
await nyc.addAllFiles() |
| 22 |
|
| 23 |
const notLoadedPath = path.join(fixtures, './not-loaded.js') |
| 24 |
const reports = (await nyc.coverageData()).filter(report => ap(report)[notLoadedPath]) |
| 25 |
const report = reports[0][notLoadedPath] |
| 26 |
|
| 27 |
t.strictEqual(reports.length, 1) |
| 28 |
t.strictEqual(report.s['0'], 0) |
| 29 |
t.strictEqual(report.s['1'], 0) |
| 30 |
}) |
| 31 |
|
| 32 |
t.test('outputs an empty coverage report for multiple configured extensions', async t => { |
| 33 |
const cwd = path.resolve(fixtures, './conf-multiple-extensions') |
| 34 |
const nyc = new NYC(await parseArgv(cwd)) |
| 35 |
await nyc.reset() |
| 36 |
await nyc.addAllFiles() |
| 37 |
|
| 38 |
const notLoadedPath1 = path.join(cwd, './not-loaded.es6') |
| 39 |
const notLoadedPath2 = path.join(cwd, './not-loaded.js') |
| 40 |
const reports = (await nyc.coverageData()).filter(report => { |
| 41 |
const apr = ap(report) |
| 42 |
return apr[notLoadedPath1] || apr[notLoadedPath2] |
| 43 |
}) |
| 44 |
|
| 45 |
t.strictEqual(reports.length, 1) |
| 46 |
|
| 47 |
const report1 = reports[0][notLoadedPath1] |
| 48 |
t.strictEqual(report1.s['0'], 0) |
| 49 |
t.strictEqual(report1.s['1'], 0) |
| 50 |
|
| 51 |
const report2 = reports[0][notLoadedPath2] |
| 52 |
t.strictEqual(report2.s['0'], 0) |
| 53 |
t.strictEqual(report2.s['1'], 0) |
| 54 |
}) |
| 55 |
|
| 56 |
t.test('transpiles .js files added via addAllFiles', async t => { |
| 57 |
await fs.writeFile( |
| 58 |
'./test/fixtures/needs-transpile.js', |
| 59 |
'--> pork chop sandwiches <--\nconst a = 99', |
| 60 |
'utf-8' |
| 61 |
) |
| 62 |
|
| 63 |
const nyc = new NYC(await parseArgv(fixtures, [ |
| 64 |
'--use-spawn-wrap=true', |
| 65 |
'--require', |
| 66 |
transpileHook |
| 67 |
])) |
| 68 |
await nyc.reset() |
| 69 |
await nyc.addAllFiles() |
| 70 |
|
| 71 |
const needsTranspilePath = path.join(fixtures, './needs-transpile.js') |
| 72 |
const reports = (await nyc.coverageData()).filter(report => ap(report)[needsTranspilePath]) |
| 73 |
const report = reports[0][needsTranspilePath] |
| 74 |
|
| 75 |
t.strictEqual(reports.length, 1) |
| 76 |
t.strictEqual(report.s['0'], 0) |
| 77 |
|
| 78 |
await fs.unlink(needsTranspilePath) |
| 79 |
}) |
| 80 |
|
| 81 |
t.test('does not attempt to transpile files when they are excluded', async t => { |
| 82 |
const notNeedTranspilePath = path.join(fixtures, './do-not-need-transpile.do-not-transpile') |
| 83 |
await fs.writeFile( |
| 84 |
notNeedTranspilePath, |
| 85 |
'--> pork chop sandwiches <--\nconst a = 99', |
| 86 |
'utf-8' |
| 87 |
) |
| 88 |
|
| 89 |
const nyc = new NYC(await parseArgv(fixtures, [ |
| 90 |
'--use-spawn-wrap=true', |
| 91 |
`--require=${transpileHook}`, |
| 92 |
'--extension=.do-not-transpile', |
| 93 |
'--include=needs-transpile.do-not-transpile' |
| 94 |
])) |
| 95 |
|
| 96 |
await nyc.reset() |
| 97 |
|
| 98 |
// If this ran against *.do-not-transpile it would throw |
| 99 |
await nyc.addAllFiles() |
| 100 |
await fs.unlink(notNeedTranspilePath) |
| 101 |
}) |
| 102 |
|
| 103 |
t.test('transpiles non-.js files added via addAllFiles', async t => { |
| 104 |
await fs.writeFile( |
| 105 |
'./test/fixtures/needs-transpile.whatever', |
| 106 |
'--> pork chop sandwiches <--\nconst a = 99', |
| 107 |
'utf-8' |
| 108 |
) |
| 109 |
|
| 110 |
const nyc = new NYC(await parseArgv(fixtures, [ |
| 111 |
'--use-spawn-wrap=true', |
| 112 |
`--require=${transpileHook}`, |
| 113 |
'--extension=.whatever' |
| 114 |
])) |
| 115 |
|
| 116 |
await nyc.reset() |
| 117 |
await nyc.addAllFiles() |
| 118 |
|
| 119 |
const needsTranspilePath = path.join(fixtures, './needs-transpile.whatever') |
| 120 |
const reports = (await nyc.coverageData()).filter(report => ap(report)[needsTranspilePath]) |
| 121 |
const report = reports[0][needsTranspilePath] |
| 122 |
|
| 123 |
t.strictEqual(reports.length, 1) |
| 124 |
t.strictEqual(report.s['0'], 0) |
| 125 |
|
| 126 |
await fs.unlink(needsTranspilePath) |
| 127 |
}) |