Skip to content

Commit

Permalink
fix: Always call coverage initialization function (#524)
Browse files Browse the repository at this point in the history
Ensure that sources initialize coverage data even if no code is run.
  • Loading branch information
coreyfarrell committed Feb 3, 2020
1 parent 96efed9 commit c6536c1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
16 changes: 6 additions & 10 deletions packages/istanbul-lib-instrument/src/visitor.js
Expand Up @@ -28,7 +28,6 @@ class VisitState {
ignoreClassMethods = []
) {
this.varName = genVar(sourceFilePath);
this.varCalled = false;
this.attrs = {};
this.nextIgnore = null;
this.cov = new SourceCoverage(sourceFilePath);
Expand Down Expand Up @@ -168,7 +167,6 @@ class VisitState {
? // If `index` present, turn `x` into `x[index]`.
x => T.memberExpression(x, T.numericLiteral(index), true)
: x => x;
this.varCalled = true;
return T.updateExpression(
'++',
wrap(
Expand Down Expand Up @@ -664,14 +662,12 @@ function programVisitor(types, sourceFilePath = 'unknown.js', opts = {}) {
INITIAL: coverageNode,
HASH: T.stringLiteral(hash)
});
// explicitly call this.varName if this file has no coverage
if (!visitState.varCalled) {
path.node.body.unshift(
T.expressionStatement(
T.callExpression(T.identifier(visitState.varName), [])
)
);
}
// explicitly call this.varName to ensure coverage is always initialized
path.node.body.unshift(
T.expressionStatement(
T.callExpression(T.identifier(visitState.varName), [])
)
);
path.node.body.unshift(cv);
return {
fileCoverage: coverageData,
Expand Down
2 changes: 1 addition & 1 deletion packages/istanbul-lib-instrument/test/varia.test.js
Expand Up @@ -96,7 +96,7 @@ describe('varia', () => {
const code = v.getGeneratedCode();
assert.ok(
code.match(
/return actualCoverage;}export function fn1\(\){cov_(.+)\.f\[\d+\]\+\+;}export default function\(\){cov_(.+)\.f\[\d+\]\+\+;}/
/return actualCoverage;}cov_([^(]+)\(\);export function fn1\(\){cov_(.+)\.f\[\d+\]\+\+;}export default function\(\){cov_(.+)\.f\[\d+\]\+\+;}/
)
);
});
Expand Down

0 comments on commit c6536c1

Please sign in to comment.