Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: exclude coverage of the CJS-ESM bridge from results #83

Merged
merged 2 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/is-cjs-esm-bridge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = ({ functions }) => {
// https://github.com/nodejs/node/blob/v12.1.0/lib/internal/modules/esm/create_dynamic_module.js#L11-L19
return functions.length === 3 &&
functions[0].functionName === '' &&
functions[0].isBlockCoverage === true &&
functions[1].functionName === 'get' &&
functions[1].isBlockCoverage === false &&
functions[2].functionName === 'set' &&
functions[2].isBlockCoverage === true
}
29 changes: 26 additions & 3 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { isAbsolute, resolve } = require('path')
// TODO: switch back to @c88/v8-coverage once patch is landed.
const { mergeProcessCovs } = require('@bcoe/v8-coverage')
const v8toIstanbul = require('v8-to-istanbul')
const isCjsEsmBridgeCov = require('./is-cjs-esm-bridge')

class Report {
constructor ({
Expand Down Expand Up @@ -55,21 +56,43 @@ class Report {
if (this._allCoverageFiles) return this._allCoverageFiles

const v8ProcessCov = this._getMergedProcessCov()

const map = libCoverage.createCoverageMap({})
const resultCountPerPath = new Map()
const possibleCjsEsmBridges = new Map()

for (const v8ScriptCov of v8ProcessCov.result) {
try {
const path = resolve(this.resolve, v8ScriptCov.url)
const converter = v8toIstanbul(path, this.wrapperLength)
await converter.load()
converter.applyCoverage(v8ScriptCov.functions)
map.merge(converter.toIstanbul())

if (resultCountPerPath.has(path)) {
resultCountPerPath.set(path, resultCountPerPath.get(path) + 1)
} else {
resultCountPerPath.set(path, 0)
}

if (isCjsEsmBridgeCov(v8ScriptCov)) {
possibleCjsEsmBridges.set(converter, {
path,
functions: v8ScriptCov.functions
})
} else {
converter.applyCoverage(v8ScriptCov.functions)
map.merge(converter.toIstanbul())
}
} catch (err) {
console.warn(`file: ${v8ScriptCov.url} error: ${err.stack}`)
}
}

for (const [converter, { path, functions }] of possibleCjsEsmBridges) {
if (resultCountPerPath.get(path) <= 1) {
converter.applyCoverage(functions)
map.merge(converter.toIstanbul())
}
}

this._allCoverageFiles = map
return this._allCoverageFiles
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/export.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'foo';
5 changes: 3 additions & 2 deletions test/fixtures/import.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import foo from './export.mjs'
import esm from './export.mjs'
import cjs from './export.cjs'

console.info(foo())
console.log(esm(), cjs())
5 changes: 3 additions & 2 deletions test/integration.js.snap
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`c8 ESM Modules collects coverage for ESM modules 1`] = `
",bar
",bar foo
------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
------------|----------|----------|----------|----------|-------------------|
All files | 80 | 100 | 50 | 80 | |
All files | 83.33 | 100 | 66.67 | 83.33 | |
export.cjs | 100 | 100 | 100 | 100 | |
export.mjs | 71.43 | 100 | 50 | 71.43 | 2,3 |
import.mjs | 100 | 100 | 100 | 100 | |
------------|----------|----------|----------|----------|-------------------|
Expand Down