Skip to content

Commit

Permalink
fix: load configuration from process.env.NYC_CONFIG if present (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Feb 7, 2017
1 parent f77db2a commit e902924
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
24 changes: 21 additions & 3 deletions src/index.js
Expand Up @@ -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
))
}

Expand Down
54 changes: 42 additions & 12 deletions test/babel-plugin-istanbul.js
Expand Up @@ -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/)
})
})

Expand Down

0 comments on commit e902924

Please sign in to comment.