From c9883f3ec9babf5bec73a5b5e395acd4122b68d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Thu, 2 May 2024 12:00:30 +0300 Subject: [PATCH] fix(coverage): apply `vite-node`'s wrapper only to executed files (#5642) --- packages/coverage-v8/src/provider.ts | 15 ++++-- .../istanbul.report.test.ts.snap | 48 ++++++++++++++++++ .../__snapshots__/v8.report.test.ts.snap | 50 +++++++++++++++++++ test/coverage-test/src/load-outside-vite.cjs | 1 + test/coverage-test/test/coverage.test.ts | 7 +++ 5 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 test/coverage-test/src/load-outside-vite.cjs diff --git a/packages/coverage-v8/src/provider.ts b/packages/coverage-v8/src/provider.ts index 1c2c7d2543a8..60f470f614a2 100644 --- a/packages/coverage-v8/src/provider.ts +++ b/packages/coverage-v8/src/provider.ts @@ -310,10 +310,17 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage source: string originalSource: string sourceMap?: { sourcemap: EncodedSourceMap } + isExecuted: boolean }> { const filePath = normalize(fileURLToPath(url)) - const transformResult = transformResults.get(filePath) || await this.ctx.vitenode.transformRequest(filePath).catch(() => {}) + let isExecuted = true + let transformResult: FetchResult | Awaited> = transformResults.get(filePath) + + if (!transformResult) { + isExecuted = false + transformResult = await this.ctx.vitenode.transformRequest(filePath).catch(() => null) + } const map = transformResult?.map as (EncodedSourceMap | undefined) const code = transformResult?.code @@ -327,6 +334,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage // These can be uncovered files included by "all: true" or files that are loaded outside vite-node if (!map) { return { + isExecuted, source: code || sourcesContent, originalSource: sourcesContent, } @@ -337,6 +345,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage sources[0] = new URL(map.sources[0], url).href return { + isExecuted, originalSource: sourcesContent, source: code || sourcesContent, sourceMap: { @@ -368,8 +377,8 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage await Promise.all(chunk.map(async ({ url, functions }) => { const sources = await this.getSources(url, transformResults, 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 + // If file was executed by vite-node we'll need to add its wrapper + const wrapperLength = sources.isExecuted ? WRAPPER_LENGTH : 0 const converter = v8ToIstanbul(url, wrapperLength, sources, undefined, this.options.ignoreEmptyLines) await converter.load() 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 ec7ed93f4a90..c922494f037c 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 @@ -1895,6 +1895,54 @@ exports[`istanbul json report 1`] = ` }, }, }, + "/src/load-outside-vite.cjs": { + "b": {}, + "branchMap": {}, + "f": { + "0": 0, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 30, + "line": 1, + }, + "start": { + "column": 26, + "line": 1, + }, + }, + "loc": { + "end": { + "column": 35, + "line": 1, + }, + "start": { + "column": 33, + "line": 1, + }, + }, + "name": "noop", + }, + }, + "path": "/src/load-outside-vite.cjs", + "s": { + "0": 0, + }, + "statementMap": { + "0": { + "end": { + "column": 35, + "line": 1, + }, + "start": { + "column": 0, + "line": 1, + }, + }, + }, + }, "/src/multi-environment.ts": { "b": { "0": [ 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 326c2b7f6b93..82bde8fbc016 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 @@ -4345,6 +4345,56 @@ exports[`v8 json report 1`] = ` }, }, }, + "/src/load-outside-vite.cjs": { + "all": false, + "b": {}, + "branchMap": {}, + "f": { + "0": 0, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 35, + "line": 1, + }, + "start": { + "column": 17, + "line": 1, + }, + }, + "line": 1, + "loc": { + "end": { + "column": 35, + "line": 1, + }, + "start": { + "column": 17, + "line": 1, + }, + }, + "name": "noop", + }, + }, + "path": "/src/load-outside-vite.cjs", + "s": { + "0": 1, + }, + "statementMap": { + "0": { + "end": { + "column": 35, + "line": 1, + }, + "start": { + "column": 0, + "line": 1, + }, + }, + }, + }, "/src/multi-environment.ts": { "all": false, "b": { diff --git a/test/coverage-test/src/load-outside-vite.cjs b/test/coverage-test/src/load-outside-vite.cjs new file mode 100644 index 000000000000..d3036bbf192b --- /dev/null +++ b/test/coverage-test/src/load-outside-vite.cjs @@ -0,0 +1 @@ +module.exports = function noop() {} diff --git a/test/coverage-test/test/coverage.test.ts b/test/coverage-test/test/coverage.test.ts index 5bb78b2ded4b..5d2bbdb97327 100644 --- a/test/coverage-test/test/coverage.test.ts +++ b/test/coverage-test/test/coverage.test.ts @@ -84,3 +84,10 @@ test.runIf(provider === 'v8' || provider === 'custom')('pre-transpiled code with transpiled.hello() }) + +test.runIf(provider === 'v8' || provider === 'custom')('file loaded outside Vite, #5639', async () => { + const { Module: { createRequire } } = await import('node:module') + + const noop = createRequire(import.meta.url)('../src/load-outside-vite.cjs') + expect(noop).toBeTypeOf('function') +})