From 40afc5f539bd628cb134a906eae91a4f082c969d Mon Sep 17 00:00:00 2001 From: Grzegorz Abramczyk Date: Sat, 5 Jan 2019 23:34:56 +0100 Subject: [PATCH] fix: nyc processing files not covered by include when `all` is enabled. (#914) Fixes #913. Fixes #782. --- index.js | 16 ++++++++-------- test/fixtures/transpile-hook.js | 4 ++++ test/src/nyc-tap.js | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 52bcc8bba..b981fbaf1 100755 --- a/index.js +++ b/index.js @@ -178,14 +178,14 @@ NYC.prototype.addAllFiles = function () { this.fakeRequire = true this.walkAllFiles(this.cwd, function (filename) { filename = path.resolve(_this.cwd, filename) - _this.addFile(filename) - var coverage = coverageFinder() - var lastCoverage = _this.instrumenter().lastFileCoverage() - if (lastCoverage) { - filename = lastCoverage.path - } - if (lastCoverage && _this.exclude.shouldInstrument(filename)) { - coverage[filename] = lastCoverage + if (_this.exclude.shouldInstrument(filename)) { + _this.addFile(filename) + var coverage = coverageFinder() + var lastCoverage = _this.instrumenter().lastFileCoverage() + if (lastCoverage) { + filename = lastCoverage.path + coverage[filename] = lastCoverage + } } }) this.fakeRequire = false diff --git a/test/fixtures/transpile-hook.js b/test/fixtures/transpile-hook.js index e0e26e8eb..2d5d5bf94 100644 --- a/test/fixtures/transpile-hook.js +++ b/test/fixtures/transpile-hook.js @@ -9,3 +9,7 @@ require.extensions['.whatever'] = function (module, filename) { var content = fs.readFileSync(filename, 'utf8'); module._compile(content.replace('--> pork chop sandwiches <--', ''), filename); } + +require.extensions['.do-not-transpile'] = function (module, filename) { + throw new Error(`Should not transpile ${filename}`) +} \ No newline at end of file diff --git a/test/src/nyc-tap.js b/test/src/nyc-tap.js index 9e3bc6265..2e78f6207 100644 --- a/test/src/nyc-tap.js +++ b/test/src/nyc-tap.js @@ -440,6 +440,26 @@ describe('nyc', function () { return done() }) + it('Do not transpiles files when not included', 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=./test/fixtures/transpile-hook', + '--extension=.do-not-transpile', + '--include=needs-transpile.do-not-transpile' + ]))) + + nyc.reset() + nyc.addAllFiles() + fs.unlinkSync(notNeedTranspilePath) + return done() + }) + describe('cache', function () { it('handles collisions', function (done) { var nyc = new NYC(configUtil.buildYargs(fixtures).parse())