From 286e9cf79ce84f7c4392ac18693053cd877c14d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Sat, 10 Dec 2022 21:19:42 +0200 Subject: [PATCH] fix: restart on config change crashes (#2481) --- packages/vitest/src/node/cli-api.ts | 6 ++++-- packages/vitest/src/node/core.ts | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/node/cli-api.ts b/packages/vitest/src/node/cli-api.ts index d06c0e9257d5..8f104a10697e 100644 --- a/packages/vitest/src/node/cli-api.ts +++ b/packages/vitest/src/node/cli-api.ts @@ -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 { diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index f6e3295c2f20..d96a4898f317 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -54,6 +54,7 @@ export class Vitest { } private _onRestartListeners: OnServerRestartHandler[] = [] + private _onSetServer: OnServerRestartHandler[] = [] async setServer(options: UserConfig, server: ViteDevServer) { this.unregisterWatcher?.() @@ -116,6 +117,8 @@ export class Vitest { await this.cache.results.readFromCache() } catch {} + + await Promise.all(this._onSetServer.map(fn => fn())) } async initCoverageProvider() { @@ -615,4 +618,8 @@ export class Vitest { onServerRestart(fn: OnServerRestartHandler) { this._onRestartListeners.push(fn) } + + onAfterSetServer(fn: OnServerRestartHandler) { + this._onSetServer.push(fn) + } }