/
nyc-index.js
533 lines (431 loc) · 17.7 KB
/
nyc-index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/* global describe, it, beforeEach */
require('source-map-support').install({ hookRequire: true })
const fs = require('fs')
const _ = require('lodash')
const ap = require('any-path')
const configUtil = require('../self-coverage/lib/config-util')
const NYC = require('../self-coverage')
// we test exit handlers in nyc-integration.js.
NYC.prototype._wrapExit = () => {}
const path = require('path')
const rimraf = require('rimraf')
const isWindows = require('is-windows')()
const spawn = require('child_process').spawn
const fixtures = path.resolve(__dirname, './fixtures')
const bin = path.resolve(__dirname, '../self-coverage/bin/nyc')
const resetState = require('./helpers/reset-state')
require('chai').should()
require('tap').mochaGlobals()
const transpileHook = path.resolve(process.cwd(), './test/fixtures/transpile-hook')
describe('nyc', function () {
beforeEach(resetState)
describe('cwd', function () {
it('sets cwd to process.cwd() if no environment variable is set', function () {
var nyc = new NYC(configUtil.buildYargs().parse())
nyc.cwd.should.eql(process.cwd())
})
it('uses NYC_CWD environment variable for cwd if it is set', function () {
process.env.NYC_CWD = path.resolve(__dirname, './fixtures')
var nyc = new NYC(configUtil.buildYargs().parse())
nyc.cwd.should.equal(path.resolve(__dirname, './fixtures'))
})
it('will look upwards for package.json from cwd', function () {
var nyc = new NYC(configUtil.buildYargs(__dirname).parse())
nyc.cwd.should.eql(path.join(__dirname, '..'))
})
it('uses --cwd for cwd if it is set (highest priority and does not look upwards for package.json) ', function () {
var nyc = new NYC(configUtil.buildYargs(__dirname).parse(['--cwd', __dirname]))
nyc.cwd.should.eql(__dirname)
})
})
describe('config', function () {
it("loads 'exclude' patterns from package.json#nyc", function () {
var nyc = new NYC(configUtil.buildYargs(path.resolve(__dirname, './fixtures')).parse())
nyc.exclude.exclude.length.should.eql(8)
})
it("loads 'extension' patterns from package.json#nyc", function () {
var nyc = new NYC(configUtil.buildYargs(path.resolve(__dirname, './fixtures/conf-multiple-extensions')).parse())
nyc.extensions.length.should.eql(3)
})
it("ignores 'include' option if it's falsy or []", function () {
var nyc1 = new NYC(configUtil.buildYargs(path.resolve(__dirname, './fixtures/conf-empty')).parse())
nyc1.exclude.include.should.equal(false)
var nyc2 = new NYC({
include: []
})
nyc2.exclude.include.should.equal(false)
})
it("ignores 'exclude' option if it's falsy", function () {
var nyc1 = new NYC(configUtil.buildYargs(path.resolve(__dirname, './fixtures/conf-empty')).parse())
nyc1.exclude.exclude.length.should.eql(15)
})
it("allows for empty 'exclude'", function () {
var nyc2 = new NYC({ exclude: [] })
// an empty exclude still has **/node_modules/**, node_modules/** and added.
nyc2.exclude.exclude.length.should.eql(2)
})
})
describe('shouldInstrumentFile', function () {
it('should exclude appropriately with defaults', function () {
var nyc = new NYC(configUtil.buildYargs('/cwd').parse([
'--exclude=test/**',
'--exclude=test{,-*}.js',
'--exclude=**/*.test.js',
'--exclude=**/__tests__/**'
]))
// nyc always excludes "node_modules/**"
nyc.exclude.shouldInstrument('/cwd/foo.js', 'foo.js').should.equal(true)
nyc.exclude.shouldInstrument('/cwd/node_modules/bar.js', 'node_modules/bar.js').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/foo/node_modules/bar.js', 'foo/node_modules/bar.js').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/test.js', 'test.js').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/testfoo.js', 'testfoo.js').should.equal(true)
nyc.exclude.shouldInstrument('/cwd/test-foo.js', 'test-foo.js').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/lib/test.js', 'lib/test.js').should.equal(true)
nyc.exclude.shouldInstrument('/cwd/foo/bar/test.js', './test.js').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/foo/bar/test.js', '.\\test.js').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/foo/bar/foo.test.js', './foo.test.js').should.equal(false)
nyc.exclude.shouldInstrument('/cwd/foo/bar/__tests__/foo.js', './__tests__/foo.js').should.equal(false)
})
it('should exclude appropriately with config.exclude', function () {
var nyc = new NYC(configUtil.buildYargs(fixtures).parse())
// fixtures/package.json configures excludes: "blarg", "blerg"
nyc.exclude.shouldInstrument('blarg.js', 'blarg.js').should.equal(false)
nyc.exclude.shouldInstrument('blarg/foo.js', 'blarg/foo.js').should.equal(false)
nyc.exclude.shouldInstrument('blerg.js', 'blerg.js').should.equal(false)
nyc.exclude.shouldInstrument('./blerg.js', './blerg.js').should.equal(false)
nyc.exclude.shouldInstrument('./blerg.js', '.\\blerg.js').should.equal(false)
})
it('should exclude outside of the current working directory', function () {
var nyc = new NYC(configUtil.buildYargs('/cwd/foo/').parse())
nyc.exclude.shouldInstrument('/cwd/bar.js', '../bar.js').should.equal(false)
})
it('should not exclude if the current working directory is inside node_modules', function () {
var nyc = new NYC(configUtil.buildYargs('/cwd/node_modules/foo/').parse())
nyc.exclude.shouldInstrument('/cwd/node_modules/foo/bar.js', './bar.js').should.equal(true)
nyc.exclude.shouldInstrument('/cwd/node_modules/foo/bar.js', '.\\bar.js').should.equal(true)
})
it('allows files to be explicitly included, rather than excluded', function () {
var nyc = new NYC(configUtil.buildYargs('/cwd/').parse(['--include=foo.js']))
nyc.exclude.shouldInstrument('/cwd/foo.js', 'foo.js').should.equal(true)
nyc.exclude.shouldInstrument('/cwd/index.js', 'index.js').should.equal(false)
})
it('exclude overrides include', function () {
var nyc = new NYC(configUtil.buildYargs('/cwd/').parse([
'--include=foo.js',
'--include=test.js',
'--exclude=**/node_modules/**',
'--exclude=test/**',
'--exclude=test{,-*}.js'
]))
nyc.exclude.shouldInstrument('/cwd/foo.js', 'foo.js').should.equal(true)
nyc.exclude.shouldInstrument('/cwd/test.js', 'test.js').should.equal(false)
})
})
describe('wrap', function () {
it('wraps modules with coverage counters when they are required', function () {
var nyc = new NYC(configUtil.buildYargs().parse())
nyc.reset()
nyc.wrap()
var check = require('./fixtures/check-instrumented')
check().should.equal(true)
})
describe('custom require hooks are installed', function () {
it('wraps modules with coverage counters when the custom require hook compiles them', function () {
let required = false
const hook = function (module, filename) {
if (filename.indexOf('check-instrumented.js') !== -1) {
required = true
}
module._compile(fs.readFileSync(filename, 'utf8'), filename)
}
var nyc = new NYC(configUtil.buildYargs().parse())
nyc.reset()
nyc.wrap()
// install the custom require hook
require.extensions['.js'] = hook // eslint-disable-line
const check = require('./fixtures/check-instrumented')
check().should.equal(true)
// and the hook should have been called
required.should.equal(true)
})
})
describe('produce source map', function () {
it('handles stack traces', function () {
var nyc = new NYC(configUtil.buildYargs().parse('--produce-source-map'))
nyc.reset()
nyc.wrap()
var check = require('./fixtures/stack-trace')
check().should.match(/stack-trace.js:4:/)
})
it('does not handle stack traces when disabled', function () {
var nyc = new NYC(configUtil.buildYargs().parse())
nyc.reset()
nyc.wrap()
var check = require('./fixtures/stack-trace')
check().should.match(/stack-trace.js:1:/)
})
})
describe('compile handlers for custom extensions are assigned', function () {
it('assigns a function to custom extensions', function () {
var nyc = new NYC(configUtil.buildYargs(
path.resolve(__dirname, './fixtures/conf-multiple-extensions')
).parse())
nyc.reset()
nyc.wrap()
require.extensions['.es6'].should.be.a('function') // eslint-disable-line
require.extensions['.foo.bar'].should.be.a('function') // eslint-disable-line
// default should still exist
require.extensions['.js'].should.be.a('function') // eslint-disable-line
})
it('calls the `_handleJs` function for custom file extensions', function () {
const required = {
es6: false,
custom: false
}
var nyc = new NYC(configUtil.buildYargs(
path.resolve(__dirname, './fixtures/conf-multiple-extensions')
).parse())
nyc['_handleJs'] = (code, options) => {
if (options.filename.indexOf('check-instrumented.es6') !== -1) {
required.es6 = true
}
if (options.filename.indexOf('check-instrumented.foo.bar') !== -1) {
required.custom = true
}
return code
}
nyc.reset()
nyc.wrap()
require('./fixtures/conf-multiple-extensions/check-instrumented.es6')
require('./fixtures/conf-multiple-extensions/check-instrumented.foo.bar')
required.custom.should.equal(true)
required.es6.should.equal(true)
})
})
function testSignal (signal, done) {
var nyc = (new NYC(configUtil.buildYargs(fixtures).parse()))
var proc = spawn(process.execPath, [bin, './' + signal + '.js'], {
cwd: fixtures,
env: {},
stdio: 'ignore'
})
proc.on('close', function () {
var reports = _.filter(nyc.loadReports(), function (report) {
return report[path.join(fixtures, signal + '.js')]
})
reports.length.should.equal(1)
return done()
})
}
it('writes coverage report when process is killed with SIGTERM', function (done) {
if (isWindows) return done()
testSignal('sigterm', done)
})
it('writes coverage report when process is killed with SIGINT', function (done) {
if (isWindows) return done()
testSignal('sigint', done)
})
it('does not output coverage for files that have not been included, by default', function (done) {
var nyc = (new NYC(configUtil.buildYargs(process.cwd()).parse()))
nyc.wrap()
nyc.reset()
var reports = _.filter(nyc.loadReports(), function (report) {
return report['./test/fixtures/not-loaded.js']
})
reports.length.should.equal(0)
return done()
})
})
describe('report', function () {
it('allows coverage report to be output in an alternative directory', function (done) {
var nyc = new NYC(configUtil.buildYargs().parse(
['--report-dir=./alternative-report', '--reporter=lcov']
))
nyc.reset()
var proc = spawn(process.execPath, ['./test/fixtures/child-1.js'], {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit'
})
proc.on('close', function () {
nyc.report()
fs.existsSync('./alternative-report/lcov.info').should.equal(true)
rimraf.sync('./alternative-report')
return done()
})
})
})
describe('addAllFiles', function () {
it('outputs an empty coverage report for all files that are not excluded', function (done) {
var nyc = new NYC(configUtil.buildYargs(fixtures).parse())
nyc.reset()
nyc.addAllFiles()
var notLoadedPath = path.join(fixtures, './not-loaded.js')
var reports = _.filter(nyc.loadReports(), function (report) {
return ap(report)[notLoadedPath]
})
var report = reports[0][notLoadedPath]
reports.length.should.equal(1)
report.s['0'].should.equal(0)
report.s['1'].should.equal(0)
return done()
})
it('outputs an empty coverage report for multiple configured extensions', function (done) {
var cwd = path.resolve(fixtures, './conf-multiple-extensions')
var nyc = new NYC(configUtil.buildYargs(cwd).parse())
nyc.reset()
nyc.addAllFiles()
var notLoadedPath1 = path.join(cwd, './not-loaded.es6')
var notLoadedPath2 = path.join(cwd, './not-loaded.js')
var reports = _.filter(nyc.loadReports(), function (report) {
var apr = ap(report)
return apr[notLoadedPath1] || apr[notLoadedPath2]
})
reports.length.should.equal(1)
var report1 = reports[0][notLoadedPath1]
report1.s['0'].should.equal(0)
report1.s['1'].should.equal(0)
var report2 = reports[0][notLoadedPath2]
report2.s['0'].should.equal(0)
report2.s['1'].should.equal(0)
return done()
})
it('tracks coverage appropriately once the file is required', function (done) {
var nyc = (new NYC(configUtil.buildYargs(fixtures).parse()))
nyc.reset()
nyc.wrap()
require('./fixtures/not-loaded')
nyc.writeCoverageFile()
var notLoadedPath = path.join(fixtures, './not-loaded.js')
var reports = _.filter(nyc.loadReports(), function (report) {
return report[notLoadedPath]
})
var report = reports[0][notLoadedPath]
reports.length.should.equal(1)
report.s['0'].should.equal(1)
report.s['1'].should.equal(1)
return done()
})
it('transpiles .js files added via addAllFiles', function (done) {
fs.writeFileSync(
'./test/fixtures/needs-transpile.js',
'--> pork chop sandwiches <--\nvar a = 99',
'utf-8'
)
var nyc = (new NYC(configUtil.buildYargs(fixtures).parse(['--require', transpileHook])))
nyc.reset()
nyc.addAllFiles()
var needsTranspilePath = path.join(fixtures, './needs-transpile.js')
var reports = _.filter(nyc.loadReports(), function (report) {
return ap(report)[needsTranspilePath]
})
var report = reports[0][needsTranspilePath]
reports.length.should.equal(1)
report.s['0'].should.equal(0)
fs.unlinkSync(needsTranspilePath)
return done()
})
it('does not attempt to transpile files when they are excluded', function (done) {
var notNeedTranspilePath = path.join(fixtures, './do-not-need-transpile.do-not-transpile')
fs.writeFileSync(
notNeedTranspilePath,
'--> pork chop sandwiches <--\nvar a = 99',
'utf-8'
)
var nyc = (new NYC(configUtil.buildYargs(fixtures).parse([
`--require=${transpileHook}`,
'--extension=.do-not-transpile',
'--include=needs-transpile.do-not-transpile'
])))
nyc.reset()
nyc.addAllFiles()
fs.unlinkSync(notNeedTranspilePath)
return done()
})
})
it('transpiles non-.js files added via addAllFiles', function (done) {
fs.writeFileSync(
'./test/fixtures/needs-transpile.whatever',
'--> pork chop sandwiches <--\nvar a = 99',
'utf-8'
)
var nyc = (new NYC(configUtil.buildYargs(fixtures).parse([
`--require=${transpileHook}`,
'--extension=.whatever'
])))
nyc.reset()
nyc.addAllFiles()
var needsTranspilePath = path.join(fixtures, './needs-transpile.whatever')
var reports = _.filter(nyc.loadReports(), function (report) {
return ap(report)[needsTranspilePath]
})
var report = reports[0][needsTranspilePath]
reports.length.should.equal(1)
report.s['0'].should.equal(0)
fs.unlinkSync(needsTranspilePath)
return done()
})
describe('cache', function () {
it('handles collisions', function (done) {
var nyc = new NYC(configUtil.buildYargs(fixtures).parse())
nyc.clearCache()
var args = [bin, process.execPath, './cache-collision-runner.js']
var proc = spawn(process.execPath, args, {
cwd: fixtures,
env: {}
})
proc.on('close', function (code) {
code.should.equal(0)
done()
})
})
it('handles identical files', function (done) {
var nyc = new NYC(configUtil.buildYargs(fixtures).parse())
nyc.clearCache()
var args = [bin, process.execPath, './identical-file-runner.js']
var proc = spawn(process.execPath, args, {
cwd: fixtures,
env: {}
})
proc.on('close', function (code) {
code.should.equal(0)
done()
})
})
})
describe('_disableCachingTransform', function () {
it('is disabled if cache is "false"', function () {
const nyc = new NYC({ cache: false })
nyc._disableCachingTransform().should.equal(true)
})
it('is enabled if cache is "true" and isChildProcess is "true"', function () {
const nyc = new NYC({
cache: true,
isChildProcess: true
})
nyc._disableCachingTransform().should.equal(false)
})
it('is disabled if cache is "true" and isChildProcess is "false"', function () {
const nyc = new NYC({
cache: true,
isChildProcess: true
})
nyc._disableCachingTransform().should.equal(false)
})
})
describe('cacheDirectory', function () {
it('should resolve default cache folder to absolute path', function () {
const nyc = new NYC({
cache: true
})
path.isAbsolute(nyc.cacheDirectory).should.equal(true)
})
it('should resolve custom cache folder to absolute path', function () {
const nyc = new NYC({
cacheDir: '.nyc_cache',
cache: true
})
path.isAbsolute(nyc.cacheDirectory).should.equal(true)
})
})
})