diff --git a/README.md b/README.md index c8b5509aa..d7dd8f5f2 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,7 @@ Any configuration options that can be set via the command line can also be speci "exclude": [ "src/**/*.spec.js" ], + "ignore-class-method": "methodToIgnore", "reporter": [ "lcov", "text-summary" @@ -344,6 +345,19 @@ hints: * `/* istanbul ignore file */`: ignore an entire source-file (this should be placed at the top of the file). +## Ignoring Methods + +There may be some methods that you want to universally ignore out of your classes +rather than having to ignore every instance of that method: + +```json +{ + "nyc": { + "ignore-class-method": "render" + } +} +``` + ## Integrating with coveralls [coveralls.io](https://coveralls.io) is a great tool for adding diff --git a/index.js b/index.js index b1ad5204a..03b504ea3 100755 --- a/index.js +++ b/index.js @@ -136,6 +136,7 @@ NYC.prototype.instrumenter = function () { NYC.prototype._createInstrumenter = function () { return this._instrumenterLib(this.cwd, { + ignoreClassMethods: [].concat(this.config.ignoreClassMethod).filter(a => a), produceSourceMap: this.config.produceSourceMap, compact: this.config.compact, preserveComments: this.config.preserveComments diff --git a/lib/instrumenters/istanbul.js b/lib/instrumenters/istanbul.js index 2d116a3ce..2cc1209aa 100644 --- a/lib/instrumenters/istanbul.js +++ b/lib/instrumenters/istanbul.js @@ -12,6 +12,7 @@ function InstrumenterIstanbul (cwd, options) { compact: options.compact, preserveComments: options.preserveComments, produceSourceMap: options.produceSourceMap, + ignoreClassMethods: options.ignoreClassMethods, esModules: true }) diff --git a/package.json b/package.json index d4fa0a5f7..218300f78 100644 --- a/package.json +++ b/package.json @@ -85,7 +85,7 @@ "glob": "^7.0.6", "istanbul-lib-coverage": "^1.1.2", "istanbul-lib-hook": "^1.1.0", - "istanbul-lib-instrument": "^1.9.2", + "istanbul-lib-instrument": "^1.10.0", "istanbul-lib-report": "^1.1.3", "istanbul-lib-source-maps": "^1.2.3", "istanbul-reports": "^1.1.4", diff --git a/test/fixtures/cli/classes.js b/test/fixtures/cli/classes.js new file mode 100644 index 000000000..e9157a63f --- /dev/null +++ b/test/fixtures/cli/classes.js @@ -0,0 +1,15 @@ +'use strict' + +class Funclass { + hit() { + const miss = () => { + console.log('This is intentionally uncovered'); + } + } + + skip() { + console.log('this will be skipped'); + } +} + +new Funclass().hit(); diff --git a/test/nyc-bin.js b/test/nyc-bin.js index 03304d52f..8e0ee4cfd 100644 --- a/test/nyc-bin.js +++ b/test/nyc-bin.js @@ -71,6 +71,29 @@ describe('the nyc cli', function () { }) }) + describe('--ignore-class-method', function () { + it('skips methods that match ignored name but still catches those that are not', function (done) { + var args = [bin, '--all', '--ignore-class-method', 'skip', process.execPath, './classes.js'] + + var proc = spawn(process.execPath, args, { + cwd: fixturesCLI, + env: env + }) + + var stdout = '' + proc.stdout.on('data', function (chunk) { + stdout += chunk + }) + + proc.on('close', function (code) { + code.should.equal(0) + var classesOutput = (stdout.match(/^(.*classes\.js).*$/m) || ['no result found'])[0] + classesOutput.should.match(/6 \|/) + done() + }) + }) + }) + describe('--check-coverage', function () { it('fails when the expected coverage is below a threshold', function (done) { var args = [bin, '--check-coverage', '--lines', '51', process.execPath, './half-covered.js'] @@ -379,7 +402,6 @@ describe('the nyc cli', function () { }) describe('coverage', function () { - if (parseInt(process.versions.node.split('.')[0]) < 4) return it('reports appropriate coverage information for es6 source files', function (done) { var args = [bin, '--reporter=lcov', '--reporter=text', process.execPath, './es6.js']