Skip to content

Commit

Permalink
feat: allow exclude/include options to be passed as Babel plugin conf…
Browse files Browse the repository at this point in the history
…ig (#16)
  • Loading branch information
insin authored and bcoe committed Jul 20, 2016
1 parent d33263c commit cf68421
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 17 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,25 @@ but you also need to __configure NYC not to instrument your code__ by adding the

## Ignoring files

You don't want to cover your test files as this will skew your coverage results. You can configure this by configuring the plugin using nyc's [exclude/include syntax](/~https://github.com/bcoe/nyc#excluding-files).
You don't want to cover your test files as this will skew your coverage results. You can configure this by providing plugin options matching nyc's [`exclude`/`include` rules](/~https://github.com/bcoe/nyc#excluding-files):

```js
{
"env": {
"test": {
"plugins": [
["istanbul", {
exclude: [
"**/*.spec.js"
]
}]
]
}
}
}
```

If you don't provide options in your Babel config, the plugin will look for `exclude`/`include` config under an `"nyc"` key in `package.json`.

You can also use [istanbul's ignore hints](/~https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes) to specify specific lines of code to skip instrumenting.

Expand Down
4 changes: 4 additions & 0 deletions fixtures/plugin-should-cover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let foo = function () {
console.log('foo')
}
foo()
4 changes: 4 additions & 0 deletions fixtures/plugin-should-not-cover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let bar = function () {
console.log('bar')
}
bar()
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ function getRealpath (n) {
}

let exclude
function shouldSkip (file) {
function shouldSkip (file, opts) {
if (!exclude) {
exclude = testExclude({
exclude = testExclude(Object.keys(opts).length > 0 ? opts : {
cwd: process.env.NYC_CWD || getRealpath(process.cwd()),
configKey: 'nyc',
configPath: dirname(findUp.sync('package.json'))
Expand All @@ -32,7 +32,7 @@ function makeVisitor ({types: t}) {
enter (path) {
this.__dv__ = null
const realPath = getRealpath(this.file.opts.filename)
if (shouldSkip(realPath)) {
if (shouldSkip(realPath, this.opts)) {
return
}
this.__dv__ = programVisitor(t, realPath)
Expand Down
52 changes: 39 additions & 13 deletions test/babel-plugin-istanbul.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,52 @@
/* global describe, it */
/* global context, describe, it */

const babel = require('babel-core')
import makeVisitor from '../src'

require('chai').should()

describe('babel-plugin-istanbul', function () {
it('should instrument file if shouldSkip returns false', function () {
var result = babel.transformFileSync('./fixtures/should-cover.js', {
plugins: [
makeVisitor({types: babel.types})
]
context('Babel plugin config', function () {
it('should instrument file if shouldSkip returns false', function () {
var result = babel.transformFileSync('./fixtures/plugin-should-cover.js', {
plugins: [
[makeVisitor({types: babel.types}), {
include: ['fixtures/plugin-should-cover.js']
}]
]
})
result.code.should.match(/statementMap/)
})

it('should not instrument file if shouldSkip returns true', function () {
var result = babel.transformFileSync('./fixtures/plugin-should-not-cover.js', {
plugins: [
[makeVisitor({types: babel.types}), {
include: ['fixtures/plugin-should-cover.js']
}]
]
})
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('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})
]
})
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/)
})
})

0 comments on commit cf68421

Please sign in to comment.