diff --git a/package.json b/package.json index 3e44182..f231759 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "babel-core": "^6.21.0", "babel-preset-es2015": "^6.18.0", "chai": "^3.5.0", - "coveralls": "^2.11.15", + "coveralls": "^2.11.16", "cross-env": "^3.1.4", "mocha": "^3.2.0", "nyc": "^10.0.0", diff --git a/src/index.js b/src/index.js index 4ce06f2..efa65b5 100644 --- a/src/index.js +++ b/src/index.js @@ -18,12 +18,30 @@ function makeShouldSkip () { return function shouldSkip (file, opts) { if (!exclude) { const cwd = getRealpath(process.env.NYC_CWD || process.cwd()) - exclude = testExclude(Object.assign( - { cwd }, - Object.keys(opts).length > 0 ? opts : { + const nycConfig = process.env.NYC_CONFIG ? JSON.parse(process.env.NYC_CONFIG) : {} + + let config = {} + if (Object.keys(opts).length > 0) { + // explicitly configuring options in babel + // takes precendence. + config = opts + } else if (nycConfig.include || nycConfig.exclude) { + // nyc was configured in a parent process (keep these settings). + config = { + include: nycConfig.include, + exclude: nycConfig.exclude + } + } else { + // fallback to loading config from key in package.json. + config = { configKey: 'nyc', configPath: dirname(findUp.sync('package.json', { cwd })) } + } + + exclude = testExclude(Object.assign( + { cwd }, + config )) } diff --git a/test/babel-plugin-istanbul.js b/test/babel-plugin-istanbul.js index 91408e3..b5e59a5 100644 --- a/test/babel-plugin-istanbul.js +++ b/test/babel-plugin-istanbul.js @@ -68,22 +68,52 @@ describe('babel-plugin-istanbul', function () { }) context('package.json "nyc" config', function () { - it('should instrument file if shouldSkip returns false', function () { - var result = babel.transformFileSync('./fixtures/should-cover.js', { - plugins: [ - makeVisitor({types: babel.types}) - ] + context('process.env.NYC_CONFIG is set', function () { + it('should instrument file if shouldSkip returns false', function () { + var result = babel.transformFileSync('./fixtures/should-cover.js', { + plugins: [ + makeVisitor({types: babel.types}) + ] + }) + result.code.should.match(/statementMap/) + }) + + it('should not instrument file if shouldSkip returns true', function () { + var result = babel.transformFileSync('./fixtures/should-not-cover.js', { + plugins: [ + makeVisitor({types: babel.types}) + ] + }) + result.code.should.not.match(/statementMap/) }) - result.code.should.match(/statementMap/) }) - it('should not instrument file if shouldSkip returns true', function () { - var result = babel.transformFileSync('./fixtures/should-not-cover.js', { - plugins: [ - makeVisitor({types: babel.types}) - ] + context('process.env.NYC_CONFIG is not set', function () { + const OLD_NYC_CONFIG = process.env.NYC_CONFIG + before(() => { + delete process.env.NYC_CONFIG + }) + after(() => { + process.env.NYC_CONFIG = OLD_NYC_CONFIG + }) + + it('should instrument file if shouldSkip returns false', function () { + var result = babel.transformFileSync('./fixtures/should-cover.js', { + plugins: [ + makeVisitor({types: babel.types}) + ] + }) + result.code.should.match(/statementMap/) + }) + + it('should not instrument file if shouldSkip returns true', function () { + var result = babel.transformFileSync('./fixtures/should-not-cover.js', { + plugins: [ + makeVisitor({types: babel.types}) + ] + }) + result.code.should.not.match(/statementMap/) }) - result.code.should.not.match(/statementMap/) }) })