[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix(coverage): v8 to ignore Vite's generated cjs import helpers (#8718)

Co-authored-by: Ari Perkkiö <ari.perkkio@gmail.com>

authored by

Bill Collins
Ari Perkkiö
and committed by
GitHub
(Oct 16, 2025, 2:59 PM +0300) 35816fe8 bc7c20de

+72
+8
pnpm-lock.yaml
··· 1377 1377 '@vitest/browser-playwright': 1378 1378 specifier: workspace:* 1379 1379 version: link:../../packages/browser-playwright 1380 + '@vitest/cjs-lib': 1381 + specifier: file:../browser/cjs-lib 1382 + version: file:test/browser/cjs-lib 1380 1383 '@vitest/coverage-istanbul': 1381 1384 specifier: workspace:* 1382 1385 version: link:../../packages/coverage-istanbul ··· 4549 4552 peerDependencies: 4550 4553 vite: ^7.1.5 4551 4554 vue: ^3.2.25 4555 + 4556 + '@vitest/cjs-lib@file:test/browser/cjs-lib': 4557 + resolution: {directory: test/browser/cjs-lib, type: directory} 4552 4558 4553 4559 '@vitest/eslint-plugin@1.3.12': 4554 4560 resolution: {integrity: sha512-cSEyUYGj8j8SLqKrzN7BlfsJ3wG67eRT25819PXuyoSBogLXiyagdKx4MHWHV1zv+EEuyMXsEKkBEKzXpxyBrg==} ··· 12599 12605 '@rolldown/pluginutils': 1.0.0-beta.29 12600 12606 vite: 7.1.5(@types/node@22.18.6)(jiti@2.5.1)(lightningcss@1.30.1)(sass-embedded@1.93.0)(sass@1.93.0)(terser@5.44.0)(tsx@4.20.5)(yaml@2.8.1) 12601 12607 vue: 3.5.21(typescript@5.9.2) 12608 + 12609 + '@vitest/cjs-lib@file:test/browser/cjs-lib': {} 12602 12610 12603 12611 '@vitest/eslint-plugin@1.3.12(eslint@9.37.0(jiti@2.5.1))(typescript@5.9.2)(vitest@packages+vitest)': 12604 12612 dependencies:
+1
test/coverage-test/package.json
··· 11 11 "@types/istanbul-lib-report": "catalog:", 12 12 "@vitejs/plugin-vue": "latest", 13 13 "@vitest/browser-playwright": "workspace:*", 14 + "@vitest/cjs-lib": "file:../browser/cjs-lib", 14 15 "@vitest/coverage-istanbul": "workspace:*", 15 16 "@vitest/coverage-v8": "workspace:*", 16 17 "@vitest/web-worker": "workspace:*",
+8
test/coverage-test/vitest.config.ts
··· 6 6 const CUSTOM_TESTS = 'test/**.custom.test.ts' 7 7 const UNIT_TESTS = 'test/**.unit.test.ts' 8 8 const BROWSER_TESTS = 'test/**.browser.test.ts' 9 + const FIXTURES = '**/fixtures/**' 9 10 10 11 const config = defineConfig({ 11 12 test: { ··· 31 32 UNIT_TESTS, 32 33 CUSTOM_TESTS, 33 34 BROWSER_TESTS, 35 + FIXTURES, 34 36 ], 35 37 }, 36 38 }, ··· 47 49 UNIT_TESTS, 48 50 CUSTOM_TESTS, 49 51 BROWSER_TESTS, 52 + FIXTURES, 50 53 ], 51 54 }, 52 55 }, ··· 58 61 name: { label: 'custom', color: 'yellow' }, 59 62 env: { COVERAGE_PROVIDER: 'custom' }, 60 63 include: [CUSTOM_TESTS], 64 + exclude: [FIXTURES], 61 65 }, 62 66 }, 63 67 ··· 87 91 '**/vue.test.ts', 88 92 '**/in-source.test.ts', 89 93 '**/query-param-transforms.test.ts', 94 + '**/test/cjs-dependency.test.ts', 90 95 ], 96 + exclude: [FIXTURES], 91 97 }, 92 98 }, 93 99 { ··· 115 121 '**/vue.test.ts', 116 122 '**/in-source.test.ts', 117 123 '**/query-param-transforms.test.ts', 124 + '**/test/cjs-dependency.test.ts', 118 125 ], 126 + exclude: [FIXTURES], 119 127 }, 120 128 }, 121 129
+14
packages/coverage-v8/src/provider.ts
··· 244 244 return true 245 245 } 246 246 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 + 247 261 // in-source test with "if (import.meta.vitest)" 248 262 if ( 249 263 (type === 'branch' || type === 'statement')
+29
test/coverage-test/test/cjs-dependency.test.ts
··· 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 + })
+6
test/coverage-test/fixtures/src/cjs-dependency.ts
··· 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 + }
+6
test/coverage-test/fixtures/test/cjs-dependency.test.ts
··· 1 + import { test } from "vitest"; 2 + import cjsDependency from "../src/cjs-dependency"; 3 + 4 + test("cjs dependency vite transforms", () => { 5 + cjsDependency() 6 + })