Skip to content

Commit ff5170c

Browse files
authored
fix(coverage): enforce order of vitest:coverage-transform plugin (#8477)
1 parent 2c2d1d4 commit ff5170c

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

packages/vitest/src/node/plugins/coverageTransform.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Vitest } from '../core'
44
export function CoverageTransform(ctx: Vitest): VitePlugin {
55
return {
66
name: 'vitest:coverage-transform',
7+
enforce: 'post',
78
transform(srcCode, id) {
89
return ctx.coverageProvider?.onFileTransform?.(
910
srcCode,

test/coverage-test/fixtures/configs/vitest.config.multi-transforms.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default defineConfig({
3737
/**
3838
* Plugin for transforming `.custom-1` and/or `.custom-2` files to Javascript
3939
*/
40-
function customFilePlugin(postfix: "1" | "2"): Plugin {
40+
export function customFilePlugin(postfix: "1" | "2"): Plugin {
4141
function transform(code: MagicString) {
4242
code.replaceAll(
4343
"<function covered>",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { expect } from 'vitest'
2+
import { customFilePlugin } from '../fixtures/configs/vitest.config.multi-transforms'
3+
import { readCoverageMap, runVitest, test } from '../utils'
4+
5+
test('custom `viteOverrides.plugins` work with `vitest:coverage-transform` plugin (#8468)', async () => {
6+
const viteOverrides = { plugins: [customFilePlugin('1')] }
7+
8+
await runVitest({
9+
include: ['fixtures/test/custom-1-syntax.test.ts'],
10+
coverage: { reporter: 'json' },
11+
}, undefined, viteOverrides)
12+
13+
const coverageMap = await readCoverageMap()
14+
expect(coverageMap.files()).toMatchInlineSnapshot(`
15+
[
16+
"<process-cwd>/fixtures/src/covered.custom-1",
17+
]
18+
`)
19+
20+
const fileCoverage = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/covered.custom-1')
21+
expect(fileCoverage).toMatchInlineSnapshot(`
22+
{
23+
"branches": "0/0 (100%)",
24+
"functions": "1/2 (50%)",
25+
"lines": "1/2 (50%)",
26+
"statements": "1/2 (50%)",
27+
}
28+
`)
29+
})

test/coverage-test/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { CoverageSummary, FileCoverageData } from 'istanbul-lib-coverage'
2+
import type { UserConfig as ViteUserConfig } from 'vite'
23
import type { TestFunction } from 'vitest'
34
import type { TestUserConfig } from 'vitest/node'
45
import { existsSync, readFileSync } from 'node:fs'
@@ -29,7 +30,7 @@ export function coverageTest(name: string, fn: TestFunction) {
2930
}
3031
}
3132

32-
export async function runVitest(config: TestUserConfig, options = { throwOnError: true }) {
33+
export async function runVitest(config: TestUserConfig, options = { throwOnError: true }, viteOverrides: ViteUserConfig = {}) {
3334
const provider = process.env.COVERAGE_PROVIDER as any
3435

3536
const result = await testUtils.runVitest({
@@ -38,6 +39,7 @@ export async function runVitest(config: TestUserConfig, options = { throwOnError
3839
...config,
3940
browser: config.browser,
4041
}, [], 'test', {
42+
...viteOverrides,
4143
test: {
4244
env: {
4345
COVERAGE_TEST: 'true',

0 commit comments

Comments
 (0)