Skip to content

Commit

Permalink
Merge 647e3c2 into 02d5927
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Apr 29, 2019
2 parents 02d5927 + 647e3c2 commit 7b1439d
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 11 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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"rimraf": "^2.6.2",
"test-exclude": "^5.0.0",
"uuid": "^3.3.2",
"v8-to-istanbul": "^2.0.4",
"v8-to-istanbul": "^2.1.0",
"yargs": "^13.1.0",
"yargs-parser": "^10.1.0"
},
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'foo';
2 changes: 1 addition & 1 deletion test/fixtures/export.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export function foo () {

export default function bar () {
return 'bar'
}
}
6 changes: 4 additions & 2 deletions test/fixtures/import.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import foo from './export.mjs'
import fooEsm from './export.mjs'
import fooCjs from './export.js'

console.info(foo())
console.info(fooEsm())
console.info(fooCjs())
4 changes: 3 additions & 1 deletion test/integration.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

exports[`c8 ESM Modules collects coverage for ESM modules 1`] = `
",bar
foo
------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
------------|----------|----------|----------|----------|-------------------|
All files | 80 | 100 | 50 | 80 | |
All files | 84.62 | 100 | 66.67 | 84.62 | |
export.js | 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 7b1439d

Please sign in to comment.