Skip to content

Commit

Permalink
Merge f321527 into 02d5927
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Apr 29, 2019
2 parents 02d5927 + f321527 commit d6a6308
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
37 changes: 34 additions & 3 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,51 @@ 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 script = v8toIstanbul(path, this.wrapperLength)
script.applyCoverage(v8ScriptCov.functions)
map.merge(script.toIstanbul())

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

// https://github.com/nodejs/node/blob/v12.0.0/lib/internal/modules/esm/create_dynamic_module.js#L12-L20
if (
v8ScriptCov.functions.length === 3 &&
v8ScriptCov.functions[0].functionName === '' &&
v8ScriptCov.functions[0].isBlockCoverage === true &&
v8ScriptCov.functions[1].functionName === 'get' &&
v8ScriptCov.functions[1].isBlockCoverage === false &&
v8ScriptCov.functions[2].functionName === 'set' &&
v8ScriptCov.functions[2].isBlockCoverage === true
) {
possibleCjsEsmBridges.set(script, {
path,
functions: v8ScriptCov.functions
})
} else {
script.applyCoverage(v8ScriptCov.functions)
map.merge(script.toIstanbul())
}
} catch (err) {
console.warn(`file: ${v8ScriptCov.url} error: ${err.stack}`)
}
}

for (const [script, { path, functions }] of possibleCjsEsmBridges) {
if (resultCountPerPath.get(path) <= 1) {
script.applyCoverage(functions)
map.merge(script.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 fooEsm from './export.mjs'
import fooCjs from './export.cjs'

console.info(foo())
console.info(fooEsm(), fooCjs())
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

0 comments on commit d6a6308

Please sign in to comment.