···16421642- **Available for providers:** `'v8'`
16431643- **CLI:** `--coverage.ignoreEmptyLines=<boolean>`
1644164416451645-Ignore empty lines, comments and other non-runtime code, e.g. Typescript types.
16451645+Ignore empty lines, comments and other non-runtime code, e.g. Typescript types. Requires `experimentalAstAwareRemapping: false`.
1646164616471647This option works only if the used compiler removes comments and other non-runtime code from the transpiled code.
16481648By default Vite uses ESBuild which removes comments and Typescript types from `.ts`, `.tsx` and `.jsx` files.
···16661666 },
16671667})
16681668```
16691669+#### coverage.experimentalAstAwareRemapping
16701670+16711671+- **Type:** `boolean`
16721672+- **Default:** `false`
16731673+- **Available for providers:** `'v8'`
16741674+- **CLI:** `--coverage.experimentalAstAwareRemapping=<boolean>`
16751675+16761676+Remap coverage with experimental AST based analysis. Provides more accurate results compared to default mode.
1669167716701678#### coverage.ignoreClassMethods
16711679
+4-7
docs/guide/coverage.md
···190190191191- [`v8`](https://github.com/istanbuljs/v8-to-istanbul#ignoring-uncovered-lines)
192192- [`ìstanbul`](https://github.com/istanbuljs/nyc#parsing-hints-ignoring-lines)
193193+- `v8` with [`experimentalAstAwareRemapping: true`](https://vitest.dev/config/#coverage-experimentalAstAwareRemapping) see [ast-v8-to-istanbul | Ignoring code](https://github.com/AriPerkkio/ast-v8-to-istanbul?tab=readme-ov-file#ignoring-code)
193194194195When using TypeScript the source codes are transpiled using `esbuild`, which strips all comments from the source codes ([esbuild#516](https://github.com/evanw/esbuild/issues/516)).
195196Comments which are considered as [legal comments](https://esbuild.github.io/api/#legal-comments) are preserved.
196197197197-For `istanbul` provider you can include a `@preserve` keyword in the ignore hint.
198198+You can include a `@preserve` keyword in the ignore hint.
198199Beware that these ignore hints may now be included in final production build as well.
199200200201```diff
201202-/* istanbul ignore if */
202203+/* istanbul ignore if -- @preserve */
203204if (condition) {
204204-```
205205206206-For `v8` this does not cause any issues. You can use `v8 ignore` comments with Typescript as usual:
207207-208208-<!-- eslint-skip -->
209209-```ts
210210-/* v8 ignore next 3 */
206206+-/* v8 ignore if */
207207++/* v8 ignore if -- @preserve */
211208if (condition) {
212209```
213210
···11import { createRequire } from 'node:module'
22import { expect } from 'vitest'
33-import { coverageTest, isV8Provider, normalizeURL, readCoverageMap, runVitest, test } from '../utils'
33+import { coverageTest, isExperimentalV8Provider, isV8Provider, normalizeURL, readCoverageMap, runVitest, test } from '../utils'
4455test('does not crash when file outside Vite is loaded (#5639)', async () => {
66 await runVitest({
···1111 const coverageMap = await readCoverageMap()
1212 const fileCoverage = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/load-outside-vite.cjs')
13131414- if (isV8Provider()) {
1414+ if (isV8Provider() || isExperimentalV8Provider()) {
1515 expect(fileCoverage).toMatchInlineSnapshot(`
1616 {
1717 "branches": "0/0 (100%)",
···2222 `)
2323 }
2424 else {
2525+ // On istanbul the instrumentation happens on Vite plugin, so files
2626+ // loaded outsite Vite should have 0% coverage
2527 expect(fileCoverage).toMatchInlineSnapshot(`
2628 {
2729 "branches": "0/0 (100%)",
+5-1
test/coverage-test/test/ignore-hints.test.ts
···44*/
5566import { expect } from 'vitest'
77-import { isV8Provider, readCoverageMap, runVitest, test } from '../utils'
77+import { isExperimentalV8Provider, isV8Provider, readCoverageMap, runVitest, test } from '../utils'
8899test('ignore hints work', async () => {
1010 await runVitest({
···2222 if (isV8Provider()) {
2323 expect(lines[15]).toBeUndefined()
2424 expect(lines[18]).toBeGreaterThanOrEqual(1)
2525+ }
2626+ else if (isExperimentalV8Provider()) {
2727+ expect(lines[15]).toBeUndefined()
2828+ expect(lines[18]).toBeUndefined()
2529 }
2630 else {
2731 expect(lines[15]).toBeGreaterThanOrEqual(1)
+1-1
test/coverage-test/fixtures/src/ignore-hints.ts
···1111// Covered line
1212second()
13131414-/* v8 ignore next -- Uncovered line v8 */
1414+/* v8 ignore next -- @preserve, Uncovered line v8 */
1515second()
16161717/* istanbul ignore next -- @preserve, Uncovered line istanbul */
+15
packages/vitest/src/node/types/coverage.ts
···275275export interface CoverageV8Options extends BaseCoverageOptions {
276276 /**
277277 * Ignore empty lines, comments and other non-runtime code, e.g. Typescript types
278278+ * - Requires `experimentalAstAwareRemapping: false`
278279 */
279280 ignoreEmptyLines?: boolean
281281+282282+ /**
283283+ * Remap coverage with experimental AST based analysis
284284+ * - Provides more accurate results compared to default mode
285285+ */
286286+ experimentalAstAwareRemapping?: boolean
287287+288288+ /**
289289+ * Set to array of class method names to ignore for coverage.
290290+ * - Requires `experimentalAstAwareRemapping: true`
291291+ *
292292+ * @default []
293293+ */
294294+ ignoreClassMethods?: string[]
280295}
281296282297export interface CustomProviderOptions