Skip to content

Commit

Permalink
feat: add support for ignoring entire files (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kent C. Dodds authored and bcoe committed Oct 21, 2017
1 parent aae256f commit f12da65
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/istanbul-lib-instrument/src/visitor.js
Expand Up @@ -5,6 +5,8 @@ import template from 'babel-template';

// istanbul ignore comment pattern
const COMMENT_RE = /^\s*istanbul\s+ignore\s+(if|else|next)(?=\W|$)/;
// istanbul ignore file pattern
const COMMENT_FILE_RE = /^\s*istanbul\s+ignore\s+(file)(?=\W|$)/;
// source map URL pattern
const SOURCE_MAP_RE = /[#@]\s*sourceMappingURL=(.*)\s*$/m;

Expand Down Expand Up @@ -483,6 +485,9 @@ const coverageTemplate = template(`
function alreadyInstrumented(path, visitState) {
return path.scope.hasBinding(visitState.varName);
}
function shouldIgnoreFile(programNode) {
return programNode.parent && programNode.parent.comments.some(c => COMMENT_FILE_RE.test(c.value));
}
/**
* programVisitor is a `babel` adaptor for instrumentation.
* It returns an object with two methods `enter` and `exit`.
Expand All @@ -508,6 +513,9 @@ function programVisitor(types, sourceFilePath = 'unknown.js', opts = {coverageVa
const visitState = new VisitState(types, sourceFilePath, opts.inputSourceMap);
return {
enter(path) {
if (shouldIgnoreFile(path.find(p => p.isProgram()))) {
return;
}
if (alreadyInstrumented(path, visitState)) {
return;
}
Expand All @@ -519,6 +527,9 @@ function programVisitor(types, sourceFilePath = 'unknown.js', opts = {coverageVa
}
visitState.cov.freeze();
const coverageData = visitState.cov.toJSON();
if (shouldIgnoreFile(path.find(p => p.isProgram()))) {
return {fileCoverage: coverageData, sourceMappingURL: visitState.sourceMappingURL};
}
coverageData[MAGIC_KEY] = MAGIC_VALUE;
const hash = createHash(SHA).update(JSON.stringify(coverageData)).digest('hex');
const coverageNode = T.valueToNode(coverageData);
Expand Down
6 changes: 5 additions & 1 deletion packages/istanbul-lib-instrument/test/specs.test.js
Expand Up @@ -62,15 +62,19 @@ function generateTests(docs) {
(doc.tests || []).forEach(function (t) {
var fn = function () {
var genOnly = (doc.opts || {}).generateOnly,
noCoverage = (doc.opts || {}).noCoverage,
v = verifier.create(doc.code, doc.opts || {}, doc.instrumentOpts, doc.inputSourceMap),
test = clone(t),
args = test.args,
out = test.out;
delete test.args;
delete test.out;
if (!genOnly) {
if (!genOnly && !noCoverage) {
v.verify(args, out, test);
}
if (noCoverage) {
assert.equal(v.code, v.generatedCode);
}
};
if (skip) {
it.skip(t.name || 'default test', fn);
Expand Down
9 changes: 9 additions & 0 deletions packages/istanbul-lib-instrument/test/specs/ignore.yaml
@@ -0,0 +1,9 @@
---
name: ignore file comment
code: |
/* istanbul ignore file */
output = true === true ? "works" : "doesn't work"
opts:
noCoverage: true
tests:
- name: file is ignored
3 changes: 2 additions & 1 deletion packages/istanbul-lib-instrument/test/util/verifier.js
Expand Up @@ -94,6 +94,7 @@ function create(code, opts, instrumenterOpts, inputSourceMap) {
var debug = extractTestOption(opts, 'debug', process.env.DEBUG==="1"),
file = extractTestOption(opts, 'file', __filename),
generateOnly = extractTestOption(opts, 'generateOnly', false),
noCoverage = extractTestOption(opts, 'noCoverage', false),
quiet = extractTestOption(opts, 'quiet', false),
coverageVariable = instrumenterOpts.coverageVariable,
g = getGlobalObject(),
Expand Down Expand Up @@ -133,7 +134,7 @@ function create(code, opts, instrumenterOpts, inputSourceMap) {
verror = new Error('Error compiling\n' + annotatedCode(code) + '\n' + ex.message);
}
}
if (generateOnly) {
if (generateOnly || noCoverage) {
assert.ok(!verror);
}
return new Verifier({
Expand Down

0 comments on commit f12da65

Please sign in to comment.