diff --git a/packages/coverage-v8/src/provider.ts b/packages/coverage-v8/src/provider.ts index b9fe8a97f174..b0f230fcf261 100644 --- a/packages/coverage-v8/src/provider.ts +++ b/packages/coverage-v8/src/provider.ts @@ -110,7 +110,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage } const converted = await Promise.all(scriptCoverages.map(async ({ url, functions }) => { - const sources = await this.getSources(url) + const sources = await this.getSources(url, functions) // If no source map was found from vite-node we can assume this file was not run in the wrapper const wrapperLength = sources.sourceMap ? WRAPPER_LENGTH : 0 @@ -204,7 +204,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage })) } - private async getSources(url: string): Promise<{ + private async getSources(url: string, functions: Profiler.FunctionCoverage[] = []): Promise<{ source: string originalSource?: string sourceMap?: { sourcemap: EncodedSourceMap } @@ -217,7 +217,12 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage const map = transformResult?.map const code = transformResult?.code - const sourcesContent = map?.sourcesContent?.[0] || await fs.readFile(filePath, 'utf-8') + const sourcesContent = map?.sourcesContent?.[0] || await fs.readFile(filePath, 'utf-8').catch(() => { + // If file does not exist construct a dummy source for it. + // These can be files that were generated dynamically during the test run and were removed after it. + const length = findLongestFunctionLength(functions) + return '.'.repeat(length) + }) // These can be uncovered files included by "all: true" or files that are loaded outside vite-node if (!map) @@ -261,3 +266,14 @@ function removeViteHelpersFromSourceMaps(source: string | undefined, map: Encode return combinedMap as EncodedSourceMap } + +/** + * Find the function with highest `endOffset` to determine the length of the file + */ +function findLongestFunctionLength(functions: Profiler.FunctionCoverage[]) { + return functions.reduce((previous, current) => { + const maxEndOffset = current.ranges.reduce((endOffset, range) => Math.max(endOffset, range.endOffset), 0) + + return Math.max(previous, maxEndOffset) + }, 0) +} diff --git a/test/coverage-test/.gitignore b/test/coverage-test/.gitignore new file mode 100644 index 000000000000..dd4ba336a97b --- /dev/null +++ b/test/coverage-test/.gitignore @@ -0,0 +1 @@ +src/dynamic-file.ignore.* \ No newline at end of file diff --git a/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap b/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap index e55e5fbacbf3..a7a57eca6842 100644 --- a/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap +++ b/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap @@ -1242,6 +1242,902 @@ exports[`c8 json report 1`] = ` }, }, }, + "/src/dynamic-file-esm.ignore.mjs": { + "all": false, + "b": { + "0": [ + 1, + ], + }, + "branchMap": { + "0": { + "line": 2, + "loc": { + "end": { + "column": 1, + "line": 4, + }, + "start": { + "column": 7, + "line": 2, + }, + }, + "locations": [ + { + "end": { + "column": 1, + "line": 4, + }, + "start": { + "column": 7, + "line": 2, + }, + }, + ], + "type": "branch", + }, + }, + "f": { + "0": 1, + "1": 0, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 1, + "line": 4, + }, + "start": { + "column": 7, + "line": 2, + }, + }, + "line": 2, + "loc": { + "end": { + "column": 1, + "line": 4, + }, + "start": { + "column": 7, + "line": 2, + }, + }, + "name": "run", + }, + "1": { + "decl": { + "end": { + "column": 23, + "line": 5, + }, + "start": { + "column": 0, + "line": 5, + }, + }, + "line": 5, + "loc": { + "end": { + "column": 23, + "line": 5, + }, + "start": { + "column": 0, + "line": 5, + }, + }, + "name": "uncovered", + }, + }, + "path": "/src/dynamic-file-esm.ignore.mjs", + "s": { + "0": 1, + "1": 1, + "2": 1, + "3": 1, + "4": 0, + }, + "statementMap": { + "0": { + "end": { + "column": 87, + "line": 1, + }, + "start": { + "column": 0, + "line": 1, + }, + }, + "1": { + "end": { + "column": 23, + "line": 2, + }, + "start": { + "column": 0, + "line": 2, + }, + }, + "2": { + "end": { + "column": 23, + "line": 3, + }, + "start": { + "column": 0, + "line": 3, + }, + }, + "3": { + "end": { + "column": 1, + "line": 4, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + "4": { + "end": { + "column": 23, + "line": 5, + }, + "start": { + "column": 0, + "line": 5, + }, + }, + }, + }, + "/src/dynamic-files.ts": { + "all": false, + "b": { + "0": [ + 1, + ], + "1": [ + 0, + ], + "2": [ + 0, + ], + "3": [ + 1, + ], + "4": [ + 0, + ], + "5": [ + 0, + ], + }, + "branchMap": { + "0": { + "line": 4, + "loc": { + "end": { + "column": 1, + "line": 24, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + "locations": [ + { + "end": { + "column": 1, + "line": 24, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + ], + "type": "branch", + }, + "1": { + "line": 8, + "loc": { + "end": { + "column": 20, + "line": 8, + }, + "start": { + "column": 4, + "line": 8, + }, + }, + "locations": [ + { + "end": { + "column": 20, + "line": 8, + }, + "start": { + "column": 4, + "line": 8, + }, + }, + ], + "type": "branch", + }, + "2": { + "line": 21, + "loc": { + "end": { + "column": 48, + "line": 21, + }, + "start": { + "column": 4, + "line": 21, + }, + }, + "locations": [ + { + "end": { + "column": 48, + "line": 21, + }, + "start": { + "column": 4, + "line": 21, + }, + }, + ], + "type": "branch", + }, + "3": { + "line": 26, + "loc": { + "end": { + "column": 1, + "line": 46, + }, + "start": { + "column": 0, + "line": 26, + }, + }, + "locations": [ + { + "end": { + "column": 1, + "line": 46, + }, + "start": { + "column": 0, + "line": 26, + }, + }, + ], + "type": "branch", + }, + "4": { + "line": 30, + "loc": { + "end": { + "column": 20, + "line": 30, + }, + "start": { + "column": 4, + "line": 30, + }, + }, + "locations": [ + { + "end": { + "column": 20, + "line": 30, + }, + "start": { + "column": 4, + "line": 30, + }, + }, + ], + "type": "branch", + }, + "5": { + "line": 43, + "loc": { + "end": { + "column": 48, + "line": 43, + }, + "start": { + "column": 4, + "line": 43, + }, + }, + "locations": [ + { + "end": { + "column": 48, + "line": 43, + }, + "start": { + "column": 4, + "line": 43, + }, + }, + ], + "type": "branch", + }, + }, + "f": { + "0": 1, + "1": 1, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 1, + "line": 24, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + "line": 4, + "loc": { + "end": { + "column": 1, + "line": 24, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + "name": "runDynamicFileESM", + }, + "1": { + "decl": { + "end": { + "column": 1, + "line": 46, + }, + "start": { + "column": 0, + "line": 26, + }, + }, + "line": 26, + "loc": { + "end": { + "column": 1, + "line": 46, + }, + "start": { + "column": 0, + "line": 26, + }, + }, + "name": "runDynamicFileCJS", + }, + }, + "path": "/src/dynamic-files.ts", + "s": { + "0": 1, + "1": 1, + "10": 1, + "11": 1, + "12": 1, + "13": 1, + "14": 1, + "15": 1, + "16": 1, + "17": 1, + "18": 1, + "19": 1, + "2": 1, + "20": 1, + "21": 1, + "22": 1, + "23": 1, + "24": 1, + "25": 1, + "26": 1, + "27": 1, + "28": 1, + "29": 1, + "3": 1, + "30": 1, + "31": 1, + "32": 1, + "33": 1, + "34": 1, + "35": 1, + "36": 1, + "37": 1, + "38": 1, + "39": 1, + "4": 1, + "40": 1, + "41": 1, + "42": 1, + "43": 1, + "44": 1, + "45": 1, + "5": 1, + "6": 1, + "7": 1, + "8": 1, + "9": 1, + }, + "statementMap": { + "0": { + "end": { + "column": 59, + "line": 1, + }, + "start": { + "column": 0, + "line": 1, + }, + }, + "1": { + "end": { + "column": 43, + "line": 2, + }, + "start": { + "column": 0, + "line": 2, + }, + }, + "10": { + "end": { + "column": 37, + "line": 11, + }, + "start": { + "column": 0, + "line": 11, + }, + }, + "11": { + "end": { + "column": 23, + "line": 12, + }, + "start": { + "column": 0, + "line": 12, + }, + }, + "12": { + "end": { + "column": 23, + "line": 13, + }, + "start": { + "column": 0, + "line": 13, + }, + }, + "13": { + "end": { + "column": 1, + "line": 14, + }, + "start": { + "column": 0, + "line": 14, + }, + }, + "14": { + "end": { + "column": 23, + "line": 15, + }, + "start": { + "column": 0, + "line": 15, + }, + }, + "15": { + "end": { + "column": 20, + "line": 16, + }, + "start": { + "column": 0, + "line": 16, + }, + }, + "16": { + "end": { + "column": 0, + "line": 17, + }, + "start": { + "column": 0, + "line": 17, + }, + }, + "17": { + "end": { + "column": 40, + "line": 18, + }, + "start": { + "column": 0, + "line": 18, + }, + }, + "18": { + "end": { + "column": 0, + "line": 19, + }, + "start": { + "column": 0, + "line": 19, + }, + }, + "19": { + "end": { + "column": 31, + "line": 20, + }, + "start": { + "column": 0, + "line": 20, + }, + }, + "2": { + "end": { + "column": 0, + "line": 3, + }, + "start": { + "column": 0, + "line": 3, + }, + }, + "20": { + "end": { + "column": 48, + "line": 21, + }, + "start": { + "column": 0, + "line": 21, + }, + }, + "21": { + "end": { + "column": 0, + "line": 22, + }, + "start": { + "column": 0, + "line": 22, + }, + }, + "22": { + "end": { + "column": 18, + "line": 23, + }, + "start": { + "column": 0, + "line": 23, + }, + }, + "23": { + "end": { + "column": 1, + "line": 24, + }, + "start": { + "column": 0, + "line": 24, + }, + }, + "24": { + "end": { + "column": 0, + "line": 25, + }, + "start": { + "column": 0, + "line": 25, + }, + }, + "25": { + "end": { + "column": 43, + "line": 26, + }, + "start": { + "column": 0, + "line": 26, + }, + }, + "26": { + "end": { + "column": 84, + "line": 27, + }, + "start": { + "column": 0, + "line": 27, + }, + }, + "27": { + "end": { + "column": 0, + "line": 28, + }, + "start": { + "column": 0, + "line": 28, + }, + }, + "28": { + "end": { + "column": 27, + "line": 29, + }, + "start": { + "column": 0, + "line": 29, + }, + }, + "29": { + "end": { + "column": 20, + "line": 30, + }, + "start": { + "column": 0, + "line": 30, + }, + }, + "3": { + "end": { + "column": 43, + "line": 4, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + "30": { + "end": { + "column": 0, + "line": 31, + }, + "start": { + "column": 0, + "line": 31, + }, + }, + "31": { + "end": { + "column": 27, + "line": 32, + }, + "start": { + "column": 0, + "line": 32, + }, + }, + "32": { + "end": { + "column": 37, + "line": 33, + }, + "start": { + "column": 0, + "line": 33, + }, + }, + "33": { + "end": { + "column": 37, + "line": 34, + }, + "start": { + "column": 0, + "line": 34, + }, + }, + "34": { + "end": { + "column": 23, + "line": 35, + }, + "start": { + "column": 0, + "line": 35, + }, + }, + "35": { + "end": { + "column": 1, + "line": 36, + }, + "start": { + "column": 0, + "line": 36, + }, + }, + "36": { + "end": { + "column": 23, + "line": 37, + }, + "start": { + "column": 0, + "line": 37, + }, + }, + "37": { + "end": { + "column": 20, + "line": 38, + }, + "start": { + "column": 0, + "line": 38, + }, + }, + "38": { + "end": { + "column": 0, + "line": 39, + }, + "start": { + "column": 0, + "line": 39, + }, + }, + "39": { + "end": { + "column": 58, + "line": 40, + }, + "start": { + "column": 0, + "line": 40, + }, + }, + "4": { + "end": { + "column": 85, + "line": 5, + }, + "start": { + "column": 0, + "line": 5, + }, + }, + "40": { + "end": { + "column": 0, + "line": 41, + }, + "start": { + "column": 0, + "line": 41, + }, + }, + "41": { + "end": { + "column": 31, + "line": 42, + }, + "start": { + "column": 0, + "line": 42, + }, + }, + "42": { + "end": { + "column": 48, + "line": 43, + }, + "start": { + "column": 0, + "line": 43, + }, + }, + "43": { + "end": { + "column": 0, + "line": 44, + }, + "start": { + "column": 0, + "line": 44, + }, + }, + "44": { + "end": { + "column": 18, + "line": 45, + }, + "start": { + "column": 0, + "line": 45, + }, + }, + "45": { + "end": { + "column": 1, + "line": 46, + }, + "start": { + "column": 0, + "line": 46, + }, + }, + "5": { + "end": { + "column": 0, + "line": 6, + }, + "start": { + "column": 0, + "line": 6, + }, + }, + "6": { + "end": { + "column": 27, + "line": 7, + }, + "start": { + "column": 0, + "line": 7, + }, + }, + "7": { + "end": { + "column": 20, + "line": 8, + }, + "start": { + "column": 0, + "line": 8, + }, + }, + "8": { + "end": { + "column": 0, + "line": 9, + }, + "start": { + "column": 0, + "line": 9, + }, + }, + "9": { + "end": { + "column": 27, + "line": 10, + }, + "start": { + "column": 0, + "line": 10, + }, + }, + }, + }, "/src/function-count.ts": { "all": false, "b": { diff --git a/test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap b/test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap index 228f2acdde93..781868ce6dea 100644 --- a/test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap +++ b/test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap @@ -16,6 +16,8 @@ exports[`custom json report 1`] = ` "/src/Defined.vue", "/src/Hello.vue", "/src/another-setup.ts", + "/src/dynamic-file-esm.ignore.mjs", + "/src/dynamic-files.ts", "/src/function-count.ts", "/src/implicitElse.ts", "/src/importEnv.ts", diff --git a/test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap b/test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap index 5863cfc4a4fc..c8518058fbf1 100644 --- a/test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap +++ b/test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap @@ -519,6 +519,401 @@ exports[`istanbul json report 1`] = ` }, }, }, + "/src/dynamic-files.ts": { + "b": { + "0": [ + 0, + 0, + ], + "1": [ + 0, + 0, + ], + "2": [ + 0, + 0, + ], + "3": [ + 0, + 0, + ], + }, + "branchMap": { + "0": { + "loc": { + "end": { + "column": null, + "line": 8, + }, + "start": { + "column": 2, + "line": 7, + }, + }, + "locations": [ + { + "end": { + "column": null, + "line": 8, + }, + "start": { + "column": 2, + "line": 7, + }, + }, + { + "end": { + "column": null, + "line": 8, + }, + "start": { + "column": 2, + "line": 7, + }, + }, + ], + "type": "if", + }, + "1": { + "loc": { + "end": { + "column": null, + "line": 21, + }, + "start": { + "column": 2, + "line": 20, + }, + }, + "locations": [ + { + "end": { + "column": null, + "line": 21, + }, + "start": { + "column": 2, + "line": 20, + }, + }, + { + "end": { + "column": null, + "line": 21, + }, + "start": { + "column": 2, + "line": 20, + }, + }, + ], + "type": "if", + }, + "2": { + "loc": { + "end": { + "column": null, + "line": 30, + }, + "start": { + "column": 2, + "line": 29, + }, + }, + "locations": [ + { + "end": { + "column": null, + "line": 30, + }, + "start": { + "column": 2, + "line": 29, + }, + }, + { + "end": { + "column": null, + "line": 30, + }, + "start": { + "column": 2, + "line": 29, + }, + }, + ], + "type": "if", + }, + "3": { + "loc": { + "end": { + "column": null, + "line": 43, + }, + "start": { + "column": 2, + "line": 42, + }, + }, + "locations": [ + { + "end": { + "column": null, + "line": 43, + }, + "start": { + "column": 2, + "line": 42, + }, + }, + { + "end": { + "column": null, + "line": 43, + }, + "start": { + "column": 2, + "line": 42, + }, + }, + ], + "type": "if", + }, + }, + "f": { + "0": 0, + "1": 0, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 42, + "line": 4, + }, + "start": { + "column": 22, + "line": 4, + }, + }, + "loc": { + "end": { + "column": null, + "line": 24, + }, + "start": { + "column": 42, + "line": 4, + }, + }, + "name": "runDynamicFileESM", + }, + "1": { + "decl": { + "end": { + "column": 42, + "line": 26, + }, + "start": { + "column": 22, + "line": 26, + }, + }, + "loc": { + "end": { + "column": null, + "line": 46, + }, + "start": { + "column": 42, + "line": 26, + }, + }, + "name": "runDynamicFileCJS", + }, + }, + "path": "/src/dynamic-files.ts", + "s": { + "0": 0, + "1": 0, + "10": 0, + "11": 0, + "12": 0, + "13": 0, + "14": 0, + "15": 0, + "2": 0, + "3": 0, + "4": 0, + "5": 0, + "6": 0, + "7": 0, + "8": 0, + "9": 0, + }, + "statementMap": { + "0": { + "end": { + "column": null, + "line": 5, + }, + "start": { + "column": 19, + "line": 5, + }, + }, + "1": { + "end": { + "column": null, + "line": 8, + }, + "start": { + "column": 2, + "line": 7, + }, + }, + "10": { + "end": { + "column": null, + "line": 30, + }, + "start": { + "column": 4, + "line": 30, + }, + }, + "11": { + "end": { + "column": null, + "line": 38, + }, + "start": { + "column": 2, + "line": 32, + }, + }, + "12": { + "end": { + "column": null, + "line": 40, + }, + "start": { + "column": 18, + "line": 40, + }, + }, + "13": { + "end": { + "column": null, + "line": 43, + }, + "start": { + "column": 2, + "line": 42, + }, + }, + "14": { + "end": { + "column": null, + "line": 43, + }, + "start": { + "column": 4, + "line": 43, + }, + }, + "15": { + "end": { + "column": null, + "line": 45, + }, + "start": { + "column": 2, + "line": 45, + }, + }, + "2": { + "end": { + "column": null, + "line": 8, + }, + "start": { + "column": 4, + "line": 8, + }, + }, + "3": { + "end": { + "column": null, + "line": 16, + }, + "start": { + "column": 2, + "line": 10, + }, + }, + "4": { + "end": { + "column": null, + "line": 18, + }, + "start": { + "column": 18, + "line": 18, + }, + }, + "5": { + "end": { + "column": null, + "line": 21, + }, + "start": { + "column": 2, + "line": 20, + }, + }, + "6": { + "end": { + "column": null, + "line": 21, + }, + "start": { + "column": 4, + "line": 21, + }, + }, + "7": { + "end": { + "column": null, + "line": 23, + }, + "start": { + "column": 2, + "line": 23, + }, + }, + "8": { + "end": { + "column": null, + "line": 27, + }, + "start": { + "column": 19, + "line": 27, + }, + }, + "9": { + "end": { + "column": null, + "line": 30, + }, + "start": { + "column": 2, + "line": 29, + }, + }, + }, + }, "/src/function-count.ts": { "b": {}, "branchMap": {}, diff --git a/test/coverage-test/coverage-report-tests/__snapshots__/v8.report.test.ts.snap b/test/coverage-test/coverage-report-tests/__snapshots__/v8.report.test.ts.snap index 24a6118f7547..305c4ee624f0 100644 --- a/test/coverage-test/coverage-report-tests/__snapshots__/v8.report.test.ts.snap +++ b/test/coverage-test/coverage-report-tests/__snapshots__/v8.report.test.ts.snap @@ -1242,6 +1242,1037 @@ exports[`v8 json report 1`] = ` }, }, }, + "/src/dynamic-file-cjs.ignore.js": { + "all": false, + "b": { + "0": [ + 1, + ], + "1": [ + 1, + ], + }, + "branchMap": { + "0": { + "line": 1, + "loc": { + "end": { + "column": 175, + "line": 1, + }, + "start": { + "column": 0, + "line": 1, + }, + }, + "locations": [ + { + "end": { + "column": 175, + "line": 1, + }, + "start": { + "column": 0, + "line": 1, + }, + }, + ], + "type": "branch", + }, + "1": { + "line": 1, + "loc": { + "end": { + "column": 151, + "line": 1, + }, + "start": { + "column": 109, + "line": 1, + }, + }, + "locations": [ + { + "end": { + "column": 151, + "line": 1, + }, + "start": { + "column": 109, + "line": 1, + }, + }, + ], + "type": "branch", + }, + }, + "f": { + "0": 1, + "1": 0, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 151, + "line": 1, + }, + "start": { + "column": 109, + "line": 1, + }, + }, + "line": 1, + "loc": { + "end": { + "column": 151, + "line": 1, + }, + "start": { + "column": 109, + "line": 1, + }, + }, + "name": "run", + }, + "1": { + "decl": { + "end": { + "column": 175, + "line": 1, + }, + "start": { + "column": 152, + "line": 1, + }, + }, + "line": 1, + "loc": { + "end": { + "column": 175, + "line": 1, + }, + "start": { + "column": 152, + "line": 1, + }, + }, + "name": "uncovered", + }, + }, + "path": "/src/dynamic-file-cjs.ignore.js", + "s": { + "0": 1, + }, + "statementMap": { + "0": { + "end": { + "column": 175, + "line": 1, + }, + "start": { + "column": 0, + "line": 1, + }, + }, + }, + }, + "/src/dynamic-file-esm.ignore.mjs": { + "all": false, + "b": { + "0": [ + 1, + ], + }, + "branchMap": { + "0": { + "line": 2, + "loc": { + "end": { + "column": 1, + "line": 4, + }, + "start": { + "column": 7, + "line": 2, + }, + }, + "locations": [ + { + "end": { + "column": 1, + "line": 4, + }, + "start": { + "column": 7, + "line": 2, + }, + }, + ], + "type": "branch", + }, + }, + "f": { + "0": 1, + "1": 0, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 1, + "line": 4, + }, + "start": { + "column": 7, + "line": 2, + }, + }, + "line": 2, + "loc": { + "end": { + "column": 1, + "line": 4, + }, + "start": { + "column": 7, + "line": 2, + }, + }, + "name": "run", + }, + "1": { + "decl": { + "end": { + "column": 23, + "line": 5, + }, + "start": { + "column": 0, + "line": 5, + }, + }, + "line": 5, + "loc": { + "end": { + "column": 23, + "line": 5, + }, + "start": { + "column": 0, + "line": 5, + }, + }, + "name": "uncovered", + }, + }, + "path": "/src/dynamic-file-esm.ignore.mjs", + "s": { + "0": 1, + "1": 1, + "2": 1, + "3": 1, + "4": 0, + }, + "statementMap": { + "0": { + "end": { + "column": 87, + "line": 1, + }, + "start": { + "column": 0, + "line": 1, + }, + }, + "1": { + "end": { + "column": 23, + "line": 2, + }, + "start": { + "column": 0, + "line": 2, + }, + }, + "2": { + "end": { + "column": 23, + "line": 3, + }, + "start": { + "column": 0, + "line": 3, + }, + }, + "3": { + "end": { + "column": 1, + "line": 4, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + "4": { + "end": { + "column": 23, + "line": 5, + }, + "start": { + "column": 0, + "line": 5, + }, + }, + }, + }, + "/src/dynamic-files.ts": { + "all": false, + "b": { + "0": [ + 1, + ], + "1": [ + 0, + ], + "2": [ + 0, + ], + "3": [ + 1, + ], + "4": [ + 0, + ], + "5": [ + 0, + ], + }, + "branchMap": { + "0": { + "line": 4, + "loc": { + "end": { + "column": 1, + "line": 24, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + "locations": [ + { + "end": { + "column": 1, + "line": 24, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + ], + "type": "branch", + }, + "1": { + "line": 8, + "loc": { + "end": { + "column": 20, + "line": 8, + }, + "start": { + "column": 4, + "line": 8, + }, + }, + "locations": [ + { + "end": { + "column": 20, + "line": 8, + }, + "start": { + "column": 4, + "line": 8, + }, + }, + ], + "type": "branch", + }, + "2": { + "line": 21, + "loc": { + "end": { + "column": 48, + "line": 21, + }, + "start": { + "column": 4, + "line": 21, + }, + }, + "locations": [ + { + "end": { + "column": 48, + "line": 21, + }, + "start": { + "column": 4, + "line": 21, + }, + }, + ], + "type": "branch", + }, + "3": { + "line": 26, + "loc": { + "end": { + "column": 1, + "line": 46, + }, + "start": { + "column": 0, + "line": 26, + }, + }, + "locations": [ + { + "end": { + "column": 1, + "line": 46, + }, + "start": { + "column": 0, + "line": 26, + }, + }, + ], + "type": "branch", + }, + "4": { + "line": 30, + "loc": { + "end": { + "column": 20, + "line": 30, + }, + "start": { + "column": 4, + "line": 30, + }, + }, + "locations": [ + { + "end": { + "column": 20, + "line": 30, + }, + "start": { + "column": 4, + "line": 30, + }, + }, + ], + "type": "branch", + }, + "5": { + "line": 43, + "loc": { + "end": { + "column": 48, + "line": 43, + }, + "start": { + "column": 4, + "line": 43, + }, + }, + "locations": [ + { + "end": { + "column": 48, + "line": 43, + }, + "start": { + "column": 4, + "line": 43, + }, + }, + ], + "type": "branch", + }, + }, + "f": { + "0": 1, + "1": 1, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 1, + "line": 24, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + "line": 4, + "loc": { + "end": { + "column": 1, + "line": 24, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + "name": "runDynamicFileESM", + }, + "1": { + "decl": { + "end": { + "column": 1, + "line": 46, + }, + "start": { + "column": 0, + "line": 26, + }, + }, + "line": 26, + "loc": { + "end": { + "column": 1, + "line": 46, + }, + "start": { + "column": 0, + "line": 26, + }, + }, + "name": "runDynamicFileCJS", + }, + }, + "path": "/src/dynamic-files.ts", + "s": { + "0": 1, + "1": 1, + "10": 1, + "11": 1, + "12": 1, + "13": 1, + "14": 1, + "15": 1, + "16": 1, + "17": 1, + "18": 1, + "19": 1, + "2": 1, + "20": 1, + "21": 1, + "22": 1, + "23": 1, + "24": 1, + "25": 1, + "26": 1, + "27": 1, + "28": 1, + "29": 1, + "3": 1, + "30": 1, + "31": 1, + "32": 1, + "33": 1, + "34": 1, + "35": 1, + "36": 1, + "37": 1, + "38": 1, + "39": 1, + "4": 1, + "40": 1, + "41": 1, + "42": 1, + "43": 1, + "44": 1, + "45": 1, + "5": 1, + "6": 1, + "7": 1, + "8": 1, + "9": 1, + }, + "statementMap": { + "0": { + "end": { + "column": 59, + "line": 1, + }, + "start": { + "column": 0, + "line": 1, + }, + }, + "1": { + "end": { + "column": 43, + "line": 2, + }, + "start": { + "column": 0, + "line": 2, + }, + }, + "10": { + "end": { + "column": 37, + "line": 11, + }, + "start": { + "column": 0, + "line": 11, + }, + }, + "11": { + "end": { + "column": 23, + "line": 12, + }, + "start": { + "column": 0, + "line": 12, + }, + }, + "12": { + "end": { + "column": 23, + "line": 13, + }, + "start": { + "column": 0, + "line": 13, + }, + }, + "13": { + "end": { + "column": 1, + "line": 14, + }, + "start": { + "column": 0, + "line": 14, + }, + }, + "14": { + "end": { + "column": 23, + "line": 15, + }, + "start": { + "column": 0, + "line": 15, + }, + }, + "15": { + "end": { + "column": 20, + "line": 16, + }, + "start": { + "column": 0, + "line": 16, + }, + }, + "16": { + "end": { + "column": 0, + "line": 17, + }, + "start": { + "column": 0, + "line": 17, + }, + }, + "17": { + "end": { + "column": 40, + "line": 18, + }, + "start": { + "column": 0, + "line": 18, + }, + }, + "18": { + "end": { + "column": 0, + "line": 19, + }, + "start": { + "column": 0, + "line": 19, + }, + }, + "19": { + "end": { + "column": 31, + "line": 20, + }, + "start": { + "column": 0, + "line": 20, + }, + }, + "2": { + "end": { + "column": 0, + "line": 3, + }, + "start": { + "column": 0, + "line": 3, + }, + }, + "20": { + "end": { + "column": 48, + "line": 21, + }, + "start": { + "column": 0, + "line": 21, + }, + }, + "21": { + "end": { + "column": 0, + "line": 22, + }, + "start": { + "column": 0, + "line": 22, + }, + }, + "22": { + "end": { + "column": 18, + "line": 23, + }, + "start": { + "column": 0, + "line": 23, + }, + }, + "23": { + "end": { + "column": 1, + "line": 24, + }, + "start": { + "column": 0, + "line": 24, + }, + }, + "24": { + "end": { + "column": 0, + "line": 25, + }, + "start": { + "column": 0, + "line": 25, + }, + }, + "25": { + "end": { + "column": 43, + "line": 26, + }, + "start": { + "column": 0, + "line": 26, + }, + }, + "26": { + "end": { + "column": 84, + "line": 27, + }, + "start": { + "column": 0, + "line": 27, + }, + }, + "27": { + "end": { + "column": 0, + "line": 28, + }, + "start": { + "column": 0, + "line": 28, + }, + }, + "28": { + "end": { + "column": 27, + "line": 29, + }, + "start": { + "column": 0, + "line": 29, + }, + }, + "29": { + "end": { + "column": 20, + "line": 30, + }, + "start": { + "column": 0, + "line": 30, + }, + }, + "3": { + "end": { + "column": 43, + "line": 4, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + "30": { + "end": { + "column": 0, + "line": 31, + }, + "start": { + "column": 0, + "line": 31, + }, + }, + "31": { + "end": { + "column": 27, + "line": 32, + }, + "start": { + "column": 0, + "line": 32, + }, + }, + "32": { + "end": { + "column": 37, + "line": 33, + }, + "start": { + "column": 0, + "line": 33, + }, + }, + "33": { + "end": { + "column": 37, + "line": 34, + }, + "start": { + "column": 0, + "line": 34, + }, + }, + "34": { + "end": { + "column": 23, + "line": 35, + }, + "start": { + "column": 0, + "line": 35, + }, + }, + "35": { + "end": { + "column": 1, + "line": 36, + }, + "start": { + "column": 0, + "line": 36, + }, + }, + "36": { + "end": { + "column": 23, + "line": 37, + }, + "start": { + "column": 0, + "line": 37, + }, + }, + "37": { + "end": { + "column": 20, + "line": 38, + }, + "start": { + "column": 0, + "line": 38, + }, + }, + "38": { + "end": { + "column": 0, + "line": 39, + }, + "start": { + "column": 0, + "line": 39, + }, + }, + "39": { + "end": { + "column": 58, + "line": 40, + }, + "start": { + "column": 0, + "line": 40, + }, + }, + "4": { + "end": { + "column": 85, + "line": 5, + }, + "start": { + "column": 0, + "line": 5, + }, + }, + "40": { + "end": { + "column": 0, + "line": 41, + }, + "start": { + "column": 0, + "line": 41, + }, + }, + "41": { + "end": { + "column": 31, + "line": 42, + }, + "start": { + "column": 0, + "line": 42, + }, + }, + "42": { + "end": { + "column": 48, + "line": 43, + }, + "start": { + "column": 0, + "line": 43, + }, + }, + "43": { + "end": { + "column": 0, + "line": 44, + }, + "start": { + "column": 0, + "line": 44, + }, + }, + "44": { + "end": { + "column": 18, + "line": 45, + }, + "start": { + "column": 0, + "line": 45, + }, + }, + "45": { + "end": { + "column": 1, + "line": 46, + }, + "start": { + "column": 0, + "line": 46, + }, + }, + "5": { + "end": { + "column": 0, + "line": 6, + }, + "start": { + "column": 0, + "line": 6, + }, + }, + "6": { + "end": { + "column": 27, + "line": 7, + }, + "start": { + "column": 0, + "line": 7, + }, + }, + "7": { + "end": { + "column": 20, + "line": 8, + }, + "start": { + "column": 0, + "line": 8, + }, + }, + "8": { + "end": { + "column": 0, + "line": 9, + }, + "start": { + "column": 0, + "line": 9, + }, + }, + "9": { + "end": { + "column": 27, + "line": 10, + }, + "start": { + "column": 0, + "line": 10, + }, + }, + }, + }, "/src/function-count.ts": { "all": false, "b": { diff --git a/test/coverage-test/src/dynamic-files.ts b/test/coverage-test/src/dynamic-files.ts new file mode 100644 index 000000000000..b4203263d640 --- /dev/null +++ b/test/coverage-test/src/dynamic-files.ts @@ -0,0 +1,46 @@ +import { existsSync, rmSync, writeFileSync } from 'node:fs' +import { createRequire } from 'node:module' + +export async function runDynamicFileESM() { + const filename = new URL('./dynamic-file-esm.ignore.mjs', import.meta.url).pathname + + if (existsSync(filename)) + rmSync(filename) + + writeFileSync(filename, ` +// File created by ${import.meta.url} +export function run() { + return "Import works" +} +function uncovered() {} + `.trim(), 'utf-8') + + const { run } = await import(filename) + + if (run() !== 'Import works') + throw new Error(`Failed to run ${filename}`) + + rmSync(filename) +} + +export async function runDynamicFileCJS() { + const filename = new URL('./dynamic-file-cjs.ignore.js', import.meta.url).pathname + + if (existsSync(filename)) + rmSync(filename) + + writeFileSync(filename, ` +// File created by ${import.meta.url} +module.exports.run = function run() { + return "Import works" +} +function uncovered() {} + `.trim(), 'utf-8') + + const { run } = createRequire(import.meta.url)(filename) + + if (run() !== 'Import works') + throw new Error(`Failed to run ${filename}`) + + rmSync(filename) +} diff --git a/test/coverage-test/test/coverage.test.ts b/test/coverage-test/test/coverage.test.ts index 73007f564392..843e156b0f9a 100644 --- a/test/coverage-test/test/coverage.test.ts +++ b/test/coverage-test/test/coverage.test.ts @@ -2,9 +2,16 @@ import { expect, test } from 'vitest' import { implicitElse } from '../src/implicitElse' import { useImportEnv } from '../src/importEnv' import { second } from '../src/function-count' +import { runDynamicFileCJS, runDynamicFileESM } from '../src/dynamic-files' + +const isBrowser = '__vitest_browser__' in globalThis + +// Browser mode crashes with dynamic files. Enable this when browser mode works. +// To keep istanbul report consistent between browser and node, skip dynamic tests when istanbul is used. +const skipDynamicFiles = process.env.COVERAGE_PROVIDER === 'istanbul' || !process.env.COVERAGE_PROVIDER const { pythagoras } = await (() => { - if ('__vitest_browser__' in globalThis) + if (isBrowser) // TODO: remove workaround after vite 4.3.2 // @ts-expect-error extension is not specified return import('../src/index') @@ -27,3 +34,11 @@ test('import meta env', () => { test('cover function counts', () => { expect(second()).toBe(2) }) + +test.skipIf(skipDynamicFiles)('run dynamic ESM file', async () => { + await runDynamicFileESM() +}) + +test.skipIf(skipDynamicFiles)('run dynamic CJS file', async () => { + await runDynamicFileCJS() +}) diff --git a/test/coverage-test/testing.mjs b/test/coverage-test/testing.mjs index 8689470a16c4..0678463cbc4b 100644 --- a/test/coverage-test/testing.mjs +++ b/test/coverage-test/testing.mjs @@ -5,6 +5,7 @@ const UPDATE_SNAPSHOTS = false const provider = process.argv[1 + process.argv.indexOf('--provider')] const isBrowser = process.argv.includes('--browser') +process.env.COVERAGE_PROVIDER = provider const threadsConfig = [{ threads: true }, { threads: false }, { singleThread: true }]