Skip to content

Commit

Permalink
fix: restart on config change crashes (#2481)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Dec 10, 2022
1 parent b51cda3 commit 286e9cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
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)
}
}

0 comments on commit 286e9cf

Please sign in to comment.