Skip to content

Commit 35816fe

Browse files
fix(coverage): v8 to ignore Vite's generated cjs import helpers (#8718)
Co-authored-by: Ari Perkkiö <[email protected]>
1 parent bc7c20d commit 35816fe

File tree

7 files changed

+72
-0
lines changed

7 files changed

+72
-0
lines changed

packages/coverage-v8/src/provider.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ export class V8CoverageProvider extends BaseCoverageProvider<ResolvedCoverageOpt
244244
return true
245245
}
246246

247+
// CJS imports as ternaries - e.g.
248+
// const React = __vite__cjsImport0_react.__esModule ? __vite__cjsImport0_react.default : __vite__cjsImport0_react;
249+
if (
250+
type === 'branch'
251+
&& node.type === 'ConditionalExpression'
252+
&& node.test.type === 'MemberExpression'
253+
&& node.test.object.type === 'Identifier'
254+
&& node.test.object.name.startsWith('__vite__cjsImport')
255+
&& node.test.property.type === 'Identifier'
256+
&& node.test.property.name === '__esModule'
257+
) {
258+
return true
259+
}
260+
247261
// in-source test with "if (import.meta.vitest)"
248262
if (
249263
(type === 'branch' || type === 'statement')

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @ts-expect-error -- types not picked up for some reason
2+
import cjsDefault from '@vitest/cjs-lib'
3+
4+
export default function getA() {
5+
return cjsDefault.a
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test } from "vitest";
2+
import cjsDependency from "../src/cjs-dependency";
3+
4+
test("cjs dependency vite transforms", () => {
5+
cjsDependency()
6+
})

test/coverage-test/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@types/istanbul-lib-report": "catalog:",
1212
"@vitejs/plugin-vue": "latest",
1313
"@vitest/browser-playwright": "workspace:*",
14+
"@vitest/cjs-lib": "file:../browser/cjs-lib",
1415
"@vitest/coverage-istanbul": "workspace:*",
1516
"@vitest/coverage-v8": "workspace:*",
1617
"@vitest/web-worker": "workspace:*",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { fileURLToPath } from 'node:url'
2+
import { expect } from 'vitest'
3+
import { readCoverageMap, runVitest, test } from '../utils'
4+
5+
test('excludes Vite transforms done for CJS dependency', async () => {
6+
await runVitest({
7+
include: ['fixtures/test/cjs-dependency.test.ts'],
8+
coverage: {
9+
reporter: 'json',
10+
},
11+
}, undefined, {
12+
cacheDir: fileURLToPath(new URL('./node_modules/.vite', import.meta.url)),
13+
optimizeDeps: { include: ['@vitest/cjs-lib', '/Users/ari/Git/vitest/test/browser/cjs-lib'] },
14+
})
15+
const coverageMap = await readCoverageMap()
16+
const files = coverageMap.files()
17+
18+
expect(files).toMatchInlineSnapshot(`
19+
[
20+
"<process-cwd>/fixtures/src/cjs-dependency.ts",
21+
]
22+
`)
23+
24+
const fileCoverage = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/cjs-dependency.ts')
25+
26+
// There should be 0 branches (#8717)
27+
expect(Object.keys(fileCoverage.b)).toHaveLength(0)
28+
expect(Object.keys(fileCoverage.branchMap)).toHaveLength(0)
29+
})

test/coverage-test/vitest.config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const ISTANBUL_TESTS = 'test/**.istanbul.test.ts'
66
const CUSTOM_TESTS = 'test/**.custom.test.ts'
77
const UNIT_TESTS = 'test/**.unit.test.ts'
88
const BROWSER_TESTS = 'test/**.browser.test.ts'
9+
const FIXTURES = '**/fixtures/**'
910

1011
const config = defineConfig({
1112
test: {
@@ -31,6 +32,7 @@ export default defineConfig({
3132
UNIT_TESTS,
3233
CUSTOM_TESTS,
3334
BROWSER_TESTS,
35+
FIXTURES,
3436
],
3537
},
3638
},
@@ -47,6 +49,7 @@ export default defineConfig({
4749
UNIT_TESTS,
4850
CUSTOM_TESTS,
4951
BROWSER_TESTS,
52+
FIXTURES,
5053
],
5154
},
5255
},
@@ -58,6 +61,7 @@ export default defineConfig({
5861
name: { label: 'custom', color: 'yellow' },
5962
env: { COVERAGE_PROVIDER: 'custom' },
6063
include: [CUSTOM_TESTS],
64+
exclude: [FIXTURES],
6165
},
6266
},
6367

@@ -87,7 +91,9 @@ export default defineConfig({
8791
'**/vue.test.ts',
8892
'**/in-source.test.ts',
8993
'**/query-param-transforms.test.ts',
94+
'**/test/cjs-dependency.test.ts',
9095
],
96+
exclude: [FIXTURES],
9197
},
9298
},
9399
{
@@ -115,7 +121,9 @@ export default defineConfig({
115121
'**/vue.test.ts',
116122
'**/in-source.test.ts',
117123
'**/query-param-transforms.test.ts',
124+
'**/test/cjs-dependency.test.ts',
118125
],
126+
exclude: [FIXTURES],
119127
},
120128
},
121129

0 commit comments

Comments
 (0)