Skip to content

Commit 498fb8b

Browse files
committed
exclude coverage of the CJS-ESM bridge from results
1 parent 0e33985 commit 498fb8b

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ const isTravisCi = process.env.TRAVIS === 'true';
3131
const codecovBashPath = process.platform === 'win32' ? join(cwd, 'coverage', uuidV4()) : null;
3232

3333
(async () => {
34+
// Remove this workaround when https://github.com/bcoe/c8/pull/83 is merged
35+
try {
36+
const patchPath = require.resolve('./patch.txt');
37+
const {readFile, unlink, writeFile} = require('fs');
38+
39+
const originalScriptPath = require.resolve('c8/lib/report.js');
40+
const originalScript = await promisify(readFile)(originalScriptPath, 'utf8');
41+
await promisify(writeFile)(originalScriptPath, originalScript.replace(
42+
`script.applyCoverage(v8ScriptCov.functions)
43+
map.merge(script.toIstanbul())`,
44+
await promisify(readFile)(patchPath, 'utf8')
45+
));
46+
await promisify(unlink)(patchPath);
47+
} catch {}
48+
3449
if (command === undefined) {
3550
require(c8BinPath);
3651
return;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"test": "node . node . test.js"
1111
},
1212
"files": [
13-
"index.js"
13+
"index.js",
14+
"patch.txt"
1415
],
1516
"bin": {
1617
"coverage": "index.js"

patch.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
if (v8ScriptCov.functions.every(({ functionName, ranges }) => {
2+
if (!script.source.includes(functionName)) {
3+
return false
4+
}
5+
6+
for (const { endOffset } of ranges) {
7+
if (endOffset > script.source.length) {
8+
return false
9+
}
10+
}
11+
12+
return true
13+
})) {
14+
script.applyCoverage(v8ScriptCov.functions)
15+
map.merge(script.toIstanbul())
16+
}

0 commit comments

Comments
 (0)