Skip to content

Commit 09ee8ea

Browse files
author
Benjamin Coe
committed
chore: add test for #420
1 parent 63a8758 commit 09ee8ea

2 files changed

Lines changed: 90 additions & 40 deletions

File tree

test/fixtures/cli/external-instrumenter.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/src/nyc-bin.js

Lines changed: 89 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/* global describe, it */
22

3+
var _ = require('lodash')
34
var path = require('path')
4-
var fs = require('fs')
5-
var spawn = require('child_process').spawn
6-
var isWindows = require('is-windows')()
5+
var bin = path.resolve(__dirname, '../../bin/nyc')
76
var fixturesCLI = path.resolve(__dirname, '../fixtures/cli')
8-
var fixturesHooks = path.resolve(__dirname, '../fixtures/hooks')
97
var fakebin = path.resolve(fixturesCLI, 'fakebin')
10-
var bin = path.resolve(__dirname, '../../bin/nyc')
8+
var fixturesHooks = path.resolve(__dirname, '../fixtures/hooks')
9+
var fs = require('fs')
10+
var glob = require('glob')
11+
var isWindows = require('is-windows')()
1112
var rimraf = require('rimraf')
13+
var spawn = require('child_process').spawn
1214

1315
require('chai').should()
1416
require('tap').mochaGlobals()
@@ -332,41 +334,6 @@ describe('the nyc cli', function () {
332334
})
333335
})
334336

335-
it('setting instrument to "false" sets noop instrumenter', function (done) {
336-
var args = [
337-
bin,
338-
'--silent',
339-
'--no-instrument',
340-
'--no-source-map',
341-
process.execPath,
342-
'./env.js'
343-
]
344-
var expected = {
345-
silent: true,
346-
instrument: false,
347-
sourceMap: false,
348-
instrumenter: './lib/instrumenters/noop'
349-
}
350-
351-
var proc = spawn(process.execPath, args, {
352-
cwd: fixturesCLI,
353-
env: env
354-
})
355-
356-
var stdout = ''
357-
proc.stdout.on('data', function (chunk) {
358-
stdout += chunk
359-
})
360-
361-
proc.on('close', function (code) {
362-
code.should.equal(0)
363-
var env = JSON.parse(stdout)
364-
var config = JSON.parse(env.NYC_CONFIG, null, 2)
365-
config.should.include(expected)
366-
done()
367-
})
368-
})
369-
370337
describe('coverage', function () {
371338
if (parseInt(process.versions.node.split('.')[0]) < 4) return
372339
it('reports appropriate coverage information for es6 source files', function (done) {
@@ -619,4 +586,86 @@ describe('the nyc cli', function () {
619586
})
620587
})
621588
})
589+
590+
describe('noop instrumenter', function () {
591+
it('setting instrument to "false" configures noop instrumenter', function (done) {
592+
var args = [
593+
bin,
594+
'--silent',
595+
'--no-instrument',
596+
'--no-source-map',
597+
process.execPath,
598+
'./env.js'
599+
]
600+
var expected = {
601+
silent: true,
602+
instrument: false,
603+
sourceMap: false,
604+
instrumenter: './lib/instrumenters/noop'
605+
}
606+
607+
var proc = spawn(process.execPath, args, {
608+
cwd: fixturesCLI,
609+
env: env
610+
})
611+
612+
var stdout = ''
613+
proc.stdout.on('data', function (chunk) {
614+
stdout += chunk
615+
})
616+
617+
proc.on('close', function (code) {
618+
code.should.equal(0)
619+
var env = JSON.parse(stdout)
620+
var config = JSON.parse(env.NYC_CONFIG, null, 2)
621+
config.should.include(expected)
622+
done()
623+
})
624+
})
625+
626+
describe('--all', function () {
627+
it('extracts coverage headers from unexecuted files', function (done) {
628+
var nycOutput = path.resolve(fixturesCLI, '.nyc_output')
629+
rimraf.sync(nycOutput)
630+
631+
var args = [
632+
bin,
633+
'--all',
634+
'--silent',
635+
'--no-instrument',
636+
'--no-source-map',
637+
process.execPath,
638+
'./es6.js'
639+
]
640+
641+
var proc = spawn(process.execPath, args, {
642+
cwd: fixturesCLI,
643+
env: env
644+
})
645+
646+
proc.on('close', function (code) {
647+
code.should.equal(0)
648+
glob(nycOutput + '/*.json', function (_err, files) {
649+
// we should have extracted the coverage header from external-instrumenter.js.
650+
var coverage = {}
651+
files.forEach(function (file) {
652+
_.assign(coverage, JSON.parse(
653+
fs.readFileSync(file, 'utf-8')
654+
))
655+
})
656+
Object.keys(coverage)[0].should.equal('./external-instrumenter.js')
657+
658+
// we should not have executed file, so all counts sould be 0.
659+
var sum = 0
660+
;Object.keys(coverage['./external-instrumenter.js'].s).forEach(function (k) {
661+
sum += coverage['./external-instrumenter.js'].s[k]
662+
})
663+
sum.should.equal(0)
664+
665+
return done()
666+
})
667+
})
668+
})
669+
})
670+
})
622671
})

0 commit comments

Comments
 (0)