Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move Vitest.setServer to post configureServer hook to enable import analysis for workspace config loading #6584

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 32 additions & 29 deletions packages/ui/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,44 @@ export default (ctx: Vitest): Plugin => {
return <Plugin>{
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,
}),
)
},
},
}
}
Expand Down
34 changes: 19 additions & 15 deletions packages/vitest/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Comment on lines +236 to +239
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, I also tried moving it to "post middleware" hook like below, but it turns out Vite doesn't await returned promise so it didn't work https://github.com/vitejs/vite/blob/95020ab49e12d143262859e095025cf02423c1d9/packages/vite/src/node/server/index.ts#L881-L884

{
  configureServer(server) {
    return async () => {
      await ctx.setServer(options, server, userConfig)
      ...
    }
  }
}

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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default [] satisfies string[];
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "a"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { test } from 'vitest';

test('test - a')
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import depAsJs from "./dep.js"

export default [
"./packages/*",
...depAsJs,
]
9 changes: 9 additions & 0 deletions test/config/test/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Loading