Skip to content

Commit a7bf2b2

Browse files
fix: collect in-source tests when the module is cached (fix #10577) (#10589)
1 parent ac2d46b commit a7bf2b2

2 files changed

Lines changed: 115 additions & 5 deletions

File tree

packages/vitest/src/runtime/runners/test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ export class TestRunner implements VitestTestRunner {
5757
}
5858

5959
importFile(filepath: string, source: VitestRunnerImportSource): unknown {
60-
if (source === 'setup') {
61-
const moduleNode = this.workerState.evaluatedModules.getModuleById(filepath)
62-
if (moduleNode) {
63-
this.workerState.evaluatedModules.invalidateModule(moduleNode)
64-
}
60+
const moduleNode = this.workerState.evaluatedModules.getModuleById(filepath)
61+
if (moduleNode && (source === 'setup' || moduleNode.evaluated)) {
62+
this.workerState.evaluatedModules.invalidateModule(moduleNode)
6563
}
6664
return this._otel.$(
6765
`vitest.module.import_${source === 'setup' ? 'setup' : 'spec'}`,
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { expect, test } from 'vitest'
2+
import { runInlineTests, StableTestFileOrderSorter, ts } from '../../test-utils'
3+
4+
// https://github.com/vitest-dev/vitest/issues/10577
5+
test('in-source tests are collected when another test file has already put the source file in the module cache', async () => {
6+
const { stderr, testTree } = await runInlineTests({
7+
'1-math.test.ts': ts`
8+
import { expect, test } from 'vitest'
9+
import { add } from './2-math.js'
10+
11+
test('add', () => {
12+
expect(add(1, 2)).toBe(3)
13+
})
14+
`,
15+
'2-math.ts': ts`
16+
export function add(a: number, b: number): number {
17+
return a + b
18+
}
19+
20+
if (import.meta.vitest) {
21+
const { test, expect } = import.meta.vitest
22+
test('add in-source', () => {
23+
expect(add(1, 2)).toBe(3)
24+
})
25+
}
26+
`,
27+
'vitest.config.ts': {
28+
test: {
29+
includeSource: ['**/*.ts'],
30+
// keep all files in a single worker so `2-math.ts` stays in the module cache
31+
isolate: false,
32+
maxWorkers: 1,
33+
},
34+
},
35+
}, {
36+
sequence: { sequencer: StableTestFileOrderSorter },
37+
})
38+
39+
expect(stderr).toBe('')
40+
expect(testTree()).toMatchInlineSnapshot(`
41+
{
42+
"1-math.test.ts": {
43+
"add": "passed",
44+
},
45+
"2-math.ts": {
46+
"add in-source": "passed",
47+
},
48+
}
49+
`)
50+
})
51+
52+
test('in-source tests in non-js files are transformed by extension-checking plugins when the module is cached', async () => {
53+
const { stderr, testTree } = await runInlineTests({
54+
'1-math.test.ts': ts`
55+
import { expect, test } from 'vitest'
56+
import { add } from './2-math.custom'
57+
58+
test('add', () => {
59+
expect(add(1, 2)).toBe(3)
60+
})
61+
`,
62+
'2-math.custom': ts`
63+
lang custom
64+
export function add(a, b) {
65+
return a + b
66+
}
67+
68+
if (import.meta.vitest) {
69+
const { test, expect } = import.meta.vitest
70+
test('add in-source', () => {
71+
expect(add(1, 2)).toBe(3)
72+
})
73+
}
74+
`,
75+
'vitest.config.ts': `
76+
import { defineConfig } from 'vitest/config'
77+
78+
export default defineConfig({
79+
plugins: [
80+
{
81+
name: 'custom-lang',
82+
transform(code, id) {
83+
if (!id.endsWith('.custom')) {
84+
return
85+
}
86+
return { code: code.replace('lang custom', ''), map: null }
87+
},
88+
},
89+
],
90+
test: {
91+
includeSource: ['**/*.custom'],
92+
isolate: false,
93+
maxWorkers: 1,
94+
},
95+
})
96+
`,
97+
}, {
98+
sequence: { sequencer: StableTestFileOrderSorter },
99+
})
100+
101+
expect(stderr).toBe('')
102+
expect(testTree()).toMatchInlineSnapshot(`
103+
{
104+
"1-math.test.ts": {
105+
"add": "passed",
106+
},
107+
"2-math.custom": {
108+
"add in-source": "passed",
109+
},
110+
}
111+
`)
112+
})

0 commit comments

Comments
 (0)
Sponsor
SponsoredKunjungi sekarang
Promo