diff --git a/lib/report.js b/lib/report.js index c8914cb32..82666bf20 100644 --- a/lib/report.js +++ b/lib/report.js @@ -62,8 +62,23 @@ class Report { try { const path = resolve(this.resolve, v8ScriptCov.url) const script = v8toIstanbul(path, this.wrapperLength) - script.applyCoverage(v8ScriptCov.functions) - map.merge(script.toIstanbul()) + + if (v8ScriptCov.functions.every(({ functionName, ranges }) => { + if (!script.source.includes(functionName)) { + return false + } + + for (const { endOffset } of ranges) { + if (endOffset > script.source.length) { + return false + } + } + + return true + })) { + script.applyCoverage(v8ScriptCov.functions) + map.merge(script.toIstanbul()) + } } catch (err) { console.warn(`file: ${v8ScriptCov.url} error: ${err.stack}`) } diff --git a/package-lock.json b/package-lock.json index c9a6cc63d..e3dc11da8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3742,9 +3742,9 @@ "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" }, "v8-to-istanbul": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-2.0.4.tgz", - "integrity": "sha512-eAHPnQYNHlZWxfa76dUrgdBK7cTmSSoX1aPq1yH/0tImW2gAGLw2GF+hhMaKAD9RvyJUP6pfSLVmV3HkoRv5oA==" + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-2.1.0.tgz", + "integrity": "sha512-3XHoQH/BjY5vO26v2puZ4hs54z5O4s5R8AfKH5ktu0hANWNtYDOFwB1iBstkGISgEpohs/U81vY8B0KbQz3N2A==" }, "validate-npm-package-license": { "version": "3.0.4", diff --git a/package.json b/package.json index 1f1bc4d13..432e53df1 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/fixtures/export-large.js b/test/fixtures/export-large.js new file mode 100644 index 000000000..1e37811e4 --- /dev/null +++ b/test/fixtures/export-large.js @@ -0,0 +1,354 @@ +module.exports = function foo() { + /* This file contains a lot of lines to test if `endOffset` of the CJS-ESM bridge is always larger than the EOF position. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ + return 'foo' +} diff --git a/test/fixtures/export.js b/test/fixtures/export.js new file mode 100644 index 000000000..809d30c3e --- /dev/null +++ b/test/fixtures/export.js @@ -0,0 +1,2 @@ +// The CJS-MJS bridge has both `set` and `get` functions in its contents +module.exports = () => 'setget'; diff --git a/test/fixtures/export.mjs b/test/fixtures/export.mjs index 491a402b6..51753ec75 100644 --- a/test/fixtures/export.mjs +++ b/test/fixtures/export.mjs @@ -4,4 +4,4 @@ export function foo () { export default function bar () { return 'bar' -} \ No newline at end of file +} diff --git a/test/fixtures/import.mjs b/test/fixtures/import.mjs index dc89146b7..22b69f7bc 100644 --- a/test/fixtures/import.mjs +++ b/test/fixtures/import.mjs @@ -1,3 +1,7 @@ -import foo from './export' +import fooEsm from './export.mjs' +import fooCjs from './export.js' +import fooCjsLarge from './export-large.js' -console.info(foo()) \ No newline at end of file +console.info(fooEsm()) +console.info(fooCjs()) +console.info(fooCjsLarge()) diff --git a/test/integration.js.snap b/test/integration.js.snap index e4bc5bf56..6623f0a91 100644 --- a/test/integration.js.snap +++ b/test/integration.js.snap @@ -2,13 +2,17 @@ exports[`c8 ESM Modules collects coverage for ESM modules 1`] = ` ",bar -------------|----------|----------|----------|----------|-------------------| -File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | -------------|----------|----------|----------|----------|-------------------| -All files | 80 | 100 | 50 | 80 | | - export.mjs | 71.43 | 100 | 50 | 71.43 | 2,3 | - import.mjs | 100 | 100 | 100 | 100 | | -------------|----------|----------|----------|----------|-------------------| +setget +foo +-----------------|----------|----------|----------|----------|-------------------| +File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | +-----------------|----------|----------|----------|----------|-------------------| +All files | 99.46 | 100 | 75 | 99.46 | | + export-large.js | 100 | 100 | 100 | 100 | | + export.js | 100 | 100 | 100 | 100 | | + export.mjs | 71.43 | 100 | 50 | 71.43 | 2,3 | + import.mjs | 100 | 100 | 100 | 100 | | +-----------------|----------|----------|----------|----------|-------------------| ," `;