From e7f35214c8fdcf218679b9ed2f6f68854c1b1e0b Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Tue, 1 Oct 2024 20:25:51 +0900 Subject: [PATCH] fix: move `Vitest.setServer` to post `configureServer` hook to enable import analysis for workspace config loading (#6584) --- packages/ui/node/index.ts | 61 ++++++++++--------- packages/vitest/src/node/plugins/index.ts | 34 ++++++----- .../workspace/config-import-analysis/dep.ts | 1 + .../packages/a/package.json | 3 + .../packages/a/test.test.ts | 3 + .../vitest.workspace.ts | 6 ++ test/config/test/workspace.test.ts | 9 +++ 7 files changed, 73 insertions(+), 44 deletions(-) create mode 100644 test/config/fixtures/workspace/config-import-analysis/dep.ts create mode 100644 test/config/fixtures/workspace/config-import-analysis/packages/a/package.json create mode 100644 test/config/fixtures/workspace/config-import-analysis/packages/a/test.test.ts create mode 100644 test/config/fixtures/workspace/config-import-analysis/vitest.workspace.ts diff --git a/packages/ui/node/index.ts b/packages/ui/node/index.ts index ef2a3fe8598c..bd2eb8a2b28a 100644 --- a/packages/ui/node/index.ts +++ b/packages/ui/node/index.ts @@ -22,41 +22,44 @@ export default (ctx: Vitest): Plugin => { return { name: 'vitest:ui', apply: 'serve', - configureServer(server) { - const uiOptions = ctx.config - const base = uiOptions.uiBase - const coverageFolder = resolveCoverageFolder(ctx) - const coveragePath = coverageFolder ? coverageFolder[1] : undefined - if (coveragePath && base === coveragePath) { - throw new Error( - `The ui base path and the coverage path cannot be the same: ${base}, change coverage.reportsDirectory`, - ) - } + configureServer: { + order: 'post', + handler(server) { + const uiOptions = ctx.config + const base = uiOptions.uiBase + const coverageFolder = resolveCoverageFolder(ctx) + const coveragePath = coverageFolder ? coverageFolder[1] : undefined + if (coveragePath && base === coveragePath) { + throw new Error( + `The ui base path and the coverage path cannot be the same: ${base}, change coverage.reportsDirectory`, + ) + } - if (coverageFolder) { + if (coverageFolder) { + server.middlewares.use( + coveragePath!, + sirv(coverageFolder[0], { + single: true, + dev: true, + setHeaders: (res) => { + res.setHeader( + 'Cache-Control', + 'public,max-age=0,must-revalidate', + ) + }, + }), + ) + } + + const clientDist = resolve(fileURLToPath(import.meta.url), '../client') server.middlewares.use( - coveragePath!, - sirv(coverageFolder[0], { + base, + sirv(clientDist, { single: true, dev: true, - setHeaders: (res) => { - res.setHeader( - 'Cache-Control', - 'public,max-age=0,must-revalidate', - ) - }, }), ) - } - - const clientDist = resolve(fileURLToPath(import.meta.url), '../client') - server.middlewares.use( - base, - sirv(clientDist, { - single: true, - dev: true, - }), - ) + }, }, } } diff --git a/packages/vitest/src/node/plugins/index.ts b/packages/vitest/src/node/plugins/index.ts index 7c1077ee88d5..81edeeff7f4c 100644 --- a/packages/vitest/src/node/plugins/index.ts +++ b/packages/vitest/src/node/plugins/index.ts @@ -233,22 +233,26 @@ export async function VitestPlugin( hijackVitePluginInject(viteConfig) }, - async configureServer(server) { - if (options.watch && process.env.VITE_TEST_WATCHER_DEBUG) { - server.watcher.on('ready', () => { - // eslint-disable-next-line no-console - console.log('[debug] watcher is ready') - }) - } - await ctx.setServer(options, server, userConfig) - if (options.api && options.watch) { - (await import('../../api/setup')).setup(ctx) - } + configureServer: { + // runs after vite:import-analysis as it relies on `server` instance on Vite 5 + order: 'post', + async handler(server) { + if (options.watch && process.env.VITE_TEST_WATCHER_DEBUG) { + server.watcher.on('ready', () => { + // eslint-disable-next-line no-console + console.log('[debug] watcher is ready') + }) + } + await ctx.setServer(options, server, userConfig) + if (options.api && options.watch) { + (await import('../../api/setup')).setup(ctx) + } - // #415, in run mode we don't need the watcher, close it would improve the performance - if (!options.watch) { - await server.watcher.close() - } + // #415, in run mode we don't need the watcher, close it would improve the performance + if (!options.watch) { + await server.watcher.close() + } + }, }, }, SsrReplacerPlugin(), diff --git a/test/config/fixtures/workspace/config-import-analysis/dep.ts b/test/config/fixtures/workspace/config-import-analysis/dep.ts new file mode 100644 index 000000000000..5c2f629922de --- /dev/null +++ b/test/config/fixtures/workspace/config-import-analysis/dep.ts @@ -0,0 +1 @@ +export default [] satisfies string[]; diff --git a/test/config/fixtures/workspace/config-import-analysis/packages/a/package.json b/test/config/fixtures/workspace/config-import-analysis/packages/a/package.json new file mode 100644 index 000000000000..44d21f1fa79e --- /dev/null +++ b/test/config/fixtures/workspace/config-import-analysis/packages/a/package.json @@ -0,0 +1,3 @@ +{ + "name": "a" +} diff --git a/test/config/fixtures/workspace/config-import-analysis/packages/a/test.test.ts b/test/config/fixtures/workspace/config-import-analysis/packages/a/test.test.ts new file mode 100644 index 000000000000..8e957631471f --- /dev/null +++ b/test/config/fixtures/workspace/config-import-analysis/packages/a/test.test.ts @@ -0,0 +1,3 @@ +import { test } from 'vitest'; + +test('test - a') diff --git a/test/config/fixtures/workspace/config-import-analysis/vitest.workspace.ts b/test/config/fixtures/workspace/config-import-analysis/vitest.workspace.ts new file mode 100644 index 000000000000..33419dd9b2e0 --- /dev/null +++ b/test/config/fixtures/workspace/config-import-analysis/vitest.workspace.ts @@ -0,0 +1,6 @@ +import depAsJs from "./dep.js" + +export default [ + "./packages/*", + ...depAsJs, +] diff --git a/test/config/test/workspace.test.ts b/test/config/test/workspace.test.ts index 44202cb41b94..92fe1723de37 100644 --- a/test/config/test/workspace.test.ts +++ b/test/config/test/workspace.test.ts @@ -81,3 +81,12 @@ it('fails if referenced file doesnt exist', async () => { `Workspace config file "vitest.workspace.ts" references a non-existing file or a directory: ${resolve('fixtures/workspace/invalid-non-existing-config/vitest.config.js')}`, ) }) + +it('vite import analysis is applied when loading workspace config', async () => { + const { stderr, stdout } = await runVitest({ + root: 'fixtures/workspace/config-import-analysis', + workspace: './fixtures/workspace/config-import-analysis/vitest.workspace.ts', + }) + expect(stderr).toBe('') + expect(stdout).toContain('test - a') +})