| 1 |
'use strict' |
| 2 |
|
| 3 |
const fs = require('fs') |
| 4 |
const path = require('path') |
| 5 |
|
| 6 |
const t = require('tap') |
| 7 |
|
| 8 |
const NYC = require('../self-coverage') |
| 9 |
|
| 10 |
const { parseArgv, resetState } = require('./helpers') |
| 11 |
|
| 12 |
// we test exit handlers in nyc-integration.js. |
| 13 |
NYC.prototype._wrapExit = () => {} |
| 14 |
|
| 15 |
const fixtures = path.resolve(__dirname, 'fixtures') |
| 16 |
const configMultExt = path.resolve(fixtures, 'conf-multiple-extensions') |
| 17 |
|
| 18 |
t.beforeEach(resetState) |
| 19 |
|
| 20 |
t.test('wraps modules with coverage counters when they are required', async t => { |
| 21 |
const nyc = new NYC(await parseArgv()) |
| 22 |
await nyc.reset() |
| 23 |
nyc.wrap() |
| 24 |
|
| 25 |
const check = require('./fixtures/check-instrumented') |
| 26 |
t.strictEqual(check(), true) |
| 27 |
}) |
| 28 |
|
| 29 |
t.test('wraps modules with coverage counters when the custom require hook compiles them', async t => { |
| 30 |
let required = false |
| 31 |
const hook = function (module, filename) { |
| 32 |
if (filename.indexOf('check-instrumented.js') !== -1) { |
| 33 |
required = true |
| 34 |
} |
| 35 |
module._compile(fs.readFileSync(filename, 'utf8'), filename) |
| 36 |
} |
| 37 |
|
| 38 |
const nyc = new NYC(await parseArgv()) |
| 39 |
await nyc.reset() |
| 40 |
nyc.wrap() |
| 41 |
|
| 42 |
// install the custom require hook |
| 43 |
require.extensions['.js'] = hook // eslint-disable-line |
| 44 |
|
| 45 |
const check = require('./fixtures/check-instrumented') |
| 46 |
t.strictEqual(check(), true) |
| 47 |
t.strictEqual(required, true) |
| 48 |
}) |
| 49 |
|
| 50 |
t.test('assigns a function to custom extensions', async t => { |
| 51 |
const nyc = new NYC(await parseArgv(configMultExt)) |
| 52 |
await nyc.reset() |
| 53 |
nyc.wrap() |
| 54 |
|
| 55 |
t.type(require.extensions['.es6'], 'function') // eslint-disable-line |
| 56 |
t.type(require.extensions['.foo.bar'], 'function') // eslint-disable-line |
| 57 |
|
| 58 |
// default should still exist |
| 59 |
t.type(require.extensions['.js'], 'function') // eslint-disable-line |
| 60 |
}) |
| 61 |
|
| 62 |
t.test('calls the `_handleJs` function for custom file extensions', async t => { |
| 63 |
const required = {} |
| 64 |
const nyc = new NYC(await parseArgv(configMultExt)) |
| 65 |
|
| 66 |
nyc._handleJs = (code, options) => { |
| 67 |
if (options.filename.includes('check-instrumented.es6')) { |
| 68 |
required.es6 = true |
| 69 |
} |
| 70 |
|
| 71 |
if (options.filename.includes('check-instrumented.foo.bar')) { |
| 72 |
required.custom = true |
| 73 |
} |
| 74 |
|
| 75 |
return code |
| 76 |
} |
| 77 |
|
| 78 |
await nyc.reset() |
| 79 |
nyc.wrap() |
| 80 |
|
| 81 |
require('./fixtures/conf-multiple-extensions/check-instrumented.es6') |
| 82 |
require('./fixtures/conf-multiple-extensions/check-instrumented.foo.bar') |
| 83 |
t.strictEqual(required.custom, true) |
| 84 |
t.strictEqual(required.es6, true) |
| 85 |
}) |
| 86 |
|
| 87 |
t.test('does not output coverage for files that have not been included, by default', async t => { |
| 88 |
const nyc = new NYC(await parseArgv(process.cwd())) |
| 89 |
nyc.wrap() |
| 90 |
await nyc.reset() |
| 91 |
|
| 92 |
const reports = (await nyc.coverageData()).filter(report => report['./test/fixtures/not-loaded.js']) |
| 93 |
t.strictEqual(reports.length, 0) |
| 94 |
}) |
| 95 |
|
| 96 |
t.test('tracks coverage appropriately once the file is required', async t => { |
| 97 |
const nyc = new NYC(await parseArgv(fixtures)) |
| 98 |
await nyc.reset() |
| 99 |
nyc.wrap() |
| 100 |
|
| 101 |
require('./fixtures/not-loaded') |
| 102 |
|
| 103 |
nyc.writeCoverageFile() |
| 104 |
|
| 105 |
const notLoadedPath = path.join(fixtures, './not-loaded.js') |
| 106 |
const reports = (await nyc.coverageData()).filter(report => report[notLoadedPath]) |
| 107 |
const report = reports[0][notLoadedPath] |
| 108 |
|
| 109 |
t.strictEqual(reports.length, 1) |
| 110 |
t.strictEqual(report.s['0'], 1) |
| 111 |
t.strictEqual(report.s['1'], 1) |
| 112 |
}) |