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: restart on config file change crashes #2481

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/vitest/src/node/cli-api.ts
Expand Up @@ -76,8 +76,10 @@ export async function startVitest(
// if it's in a CLI wrapper, exit with a special code to request restart
if (process.env.VITEST_CLI_WRAPPER)
process.exit(EXIT_CODE_RESTART)
else
ctx.start(cliFilters)
})

ctx.onAfterSetServer(() => {
ctx.start(cliFilters)
})

try {
Expand Down
7 changes: 7 additions & 0 deletions packages/vitest/src/node/core.ts
Expand Up @@ -54,6 +54,7 @@ export class Vitest {
}

private _onRestartListeners: OnServerRestartHandler[] = []
private _onSetServer: OnServerRestartHandler[] = []

async setServer(options: UserConfig, server: ViteDevServer) {
this.unregisterWatcher?.()
Expand Down Expand Up @@ -116,6 +117,8 @@ export class Vitest {
await this.cache.results.readFromCache()
}
catch {}

await Promise.all(this._onSetServer.map(fn => fn()))
}

async initCoverageProvider() {
Expand Down Expand Up @@ -615,4 +618,8 @@ export class Vitest {
onServerRestart(fn: OnServerRestartHandler) {
this._onRestartListeners.push(fn)
}

onAfterSetServer(fn: OnServerRestartHandler) {
this._onSetServer.push(fn)
}
}