Skip to content

Commit da2c945

Browse files
shinnnBenjamin E. Coe
authored andcommitted
fix: exclude coverage of the CJS-ESM bridge from results (bcoe#83)
1 parent 0dd1b04 commit da2c945

5 files changed

Lines changed: 43 additions & 7 deletions

File tree

lib/is-cjs-esm-bridge.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = ({ functions }) => {
2+
// https://github.com/nodejs/node/blob/v12.1.0/lib/internal/modules/esm/create_dynamic_module.js#L11-L19
3+
return functions.length === 3 &&
4+
functions[0].functionName === '' &&
5+
functions[0].isBlockCoverage === true &&
6+
functions[1].functionName === 'get' &&
7+
functions[1].isBlockCoverage === false &&
8+
functions[2].functionName === 'set' &&
9+
functions[2].isBlockCoverage === true
10+
}

lib/report.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { isAbsolute, resolve } = require('path')
88
// TODO: switch back to @c88/v8-coverage once patch is landed.
99
const { mergeProcessCovs } = require('@bcoe/v8-coverage')
1010
const v8toIstanbul = require('v8-to-istanbul')
11+
const isCjsEsmBridgeCov = require('./is-cjs-esm-bridge')
1112

1213
class Report {
1314
constructor ({
@@ -55,21 +56,43 @@ class Report {
5556
if (this._allCoverageFiles) return this._allCoverageFiles
5657

5758
const v8ProcessCov = this._getMergedProcessCov()
58-
5959
const map = libCoverage.createCoverageMap({})
60+
const resultCountPerPath = new Map()
61+
const possibleCjsEsmBridges = new Map()
6062

6163
for (const v8ScriptCov of v8ProcessCov.result) {
6264
try {
6365
const path = resolve(this.resolve, v8ScriptCov.url)
6466
const converter = v8toIstanbul(path, this.wrapperLength)
6567
await converter.load()
66-
converter.applyCoverage(v8ScriptCov.functions)
67-
map.merge(converter.toIstanbul())
68+
69+
if (resultCountPerPath.has(path)) {
70+
resultCountPerPath.set(path, resultCountPerPath.get(path) + 1)
71+
} else {
72+
resultCountPerPath.set(path, 0)
73+
}
74+
75+
if (isCjsEsmBridgeCov(v8ScriptCov)) {
76+
possibleCjsEsmBridges.set(converter, {
77+
path,
78+
functions: v8ScriptCov.functions
79+
})
80+
} else {
81+
converter.applyCoverage(v8ScriptCov.functions)
82+
map.merge(converter.toIstanbul())
83+
}
6884
} catch (err) {
6985
console.warn(`file: ${v8ScriptCov.url} error: ${err.stack}`)
7086
}
7187
}
7288

89+
for (const [converter, { path, functions }] of possibleCjsEsmBridges) {
90+
if (resultCountPerPath.get(path) <= 1) {
91+
converter.applyCoverage(functions)
92+
map.merge(converter.toIstanbul())
93+
}
94+
}
95+
7396
this._allCoverageFiles = map
7497
return this._allCoverageFiles
7598
}

test/fixtures/export.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = () => 'foo';

test/fixtures/import.mjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
import foo from './export.mjs'
1+
import esm from './export.mjs'
2+
import cjs from './export.cjs'
23

3-
console.info(foo())
4+
console.log(esm(), cjs())

test/integration.js.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`c8 ESM Modules collects coverage for ESM modules 1`] = `
4-
",bar
4+
",bar foo
55
------------|----------|----------|----------|----------|-------------------|
66
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
77
------------|----------|----------|----------|----------|-------------------|
8-
All files | 80 | 100 | 50 | 80 | |
8+
All files | 83.33 | 100 | 66.67 | 83.33 | |
9+
export.cjs | 100 | 100 | 100 | 100 | |
910
export.mjs | 71.43 | 100 | 50 | 71.43 | 2,3 |
1011
import.mjs | 100 | 100 | 100 | 100 | |
1112
------------|----------|----------|----------|----------|-------------------|

0 commit comments

Comments
 (0)