Skip to content

Commit

Permalink
fix: move Vitest.setServer to post configureServer hook to enable…
Browse files Browse the repository at this point in the history
… import analysis for workspace config loading (#6584)
  • Loading branch information
hi-ogawa authored Oct 1, 2024
1 parent 891d6fe commit e7f3521
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 44 deletions.
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) {
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')
})

0 comments on commit e7f3521

Please sign in to comment.