Skip to content

Commit a0dfc8b

Browse files
authored
fix(cache): allow cache hits in configureServer (#11927)
1 parent 3a76b87 commit a0dfc8b

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎packages/vitest/src/node/cache/fsModuleCache.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ export class FileSystemModuleCache {
261261
// Vite watches the files outside of the root when it loads them, a cached module skips that
262262
watchFile(file: string): void {
263263
const server = this.vitest.vite
264+
// plugins can import modules in `configureServer` before the root server is attached
265+
if (!server) {
266+
return
267+
}
264268
if (!file.startsWith(`${server.config.root}/`) && existsSync(file)) {
265269
server.watcher.add(file)
266270
}

‎test/e2e/test/ssr-runner.test.ts‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { version } from 'vite'
22
import { expect, it } from 'vitest'
3-
import { runVitest } from '../../test-utils'
3+
import { runInlineTests, runVitest } from '../../test-utils'
44

55
// https://github.com/vitest-dev/vitest/issues/9324
66
it('ssr runner.import() works in configureServer', async () => {
@@ -9,6 +9,56 @@ it('ssr runner.import() works in configureServer', async () => {
99
expect((globalThis as any).__testSsrRunner).toBe(version)
1010
})
1111

12+
it('ssr runner.import() works in configureServer with a cached module', async () => {
13+
const firstRun = await runInlineTests({
14+
'vitest.config.js': /* js */ `
15+
import { isRunnableDevEnvironment } from 'vite'
16+
import { defineConfig } from 'vitest/config'
17+
18+
export default defineConfig({
19+
plugins: [
20+
{
21+
name: 'load-routes',
22+
async configureServer(server) {
23+
const ssr = server.environments.ssr
24+
if (isRunnableDevEnvironment(ssr)) {
25+
const { routes } = await ssr.runner.import('./routes.js')
26+
if (routes.length !== 2) {
27+
throw new Error('routes were not loaded')
28+
}
29+
}
30+
},
31+
},
32+
],
33+
test: {
34+
fsModuleCache: true,
35+
fsModuleCachePath: './node_modules/.vitest-fs-cache',
36+
},
37+
})
38+
`,
39+
'routes.js': `export const routes = ['/', '/about']`,
40+
'basic.test.js': /* js */ `
41+
import { expect, test } from 'vitest'
42+
43+
test('basic', () => {
44+
expect(1 + 1).toBe(2)
45+
})
46+
`,
47+
})
48+
expect(firstRun.stderr).toBe('')
49+
await firstRun.ctx?.close()
50+
51+
const secondRun = await runVitest({ root: firstRun.root })
52+
expect(secondRun.stderr).toBe('')
53+
expect(secondRun.testTree()).toMatchInlineSnapshot(`
54+
{
55+
"basic.test.js": {
56+
"basic": "passed",
57+
},
58+
}
59+
`)
60+
})
61+
1262
it('works in project', async () => {
1363
const { stderr } = await runVitest({ root: './fixtures/ssr-runner-project' })
1464
expect(stderr).toBe('')

0 commit comments

Comments
 (0)