From 6088b372e55e94b70060da36308978ecffd2d3be Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 4 Jan 2024 17:37:33 +0100 Subject: [PATCH] fix(vitest): correctly rerun test files on change if server was restarted (#4871) --- packages/vitest/src/node/core.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/vitest/src/node/core.ts b/packages/vitest/src/node/core.ts index fcde2720a58d..6f90f2df7bf8 100644 --- a/packages/vitest/src/node/core.ts +++ b/packages/vitest/src/node/core.ts @@ -126,7 +126,10 @@ export class Vitest { const serverRestart = server.restart server.restart = async (...args) => { await Promise.all(this._onRestartListeners.map(fn => fn())) - return await serverRestart(...args) + await serverRestart(...args) + // watcher is recreated on restart + this.unregisterWatcher() + this.registerWatcher() } // since we set `server.hmr: false`, Vite does not auto restart itself @@ -136,6 +139,9 @@ export class Vitest { if (isConfig) { await Promise.all(this._onRestartListeners.map(fn => fn('config'))) await serverRestart() + // watcher is recreated on restart + this.unregisterWatcher() + this.registerWatcher() } }) } @@ -729,8 +735,15 @@ export class Vitest { } const projects = this.getModuleProjects(id) - if (!projects.length) + if (!projects.length) { + // if there are no modules it's possible that server was restarted + // we don't have information about importers anymore, so let's check if the file is a test file at least + if (this.state.filesMap.has(id) || this.projects.some(project => project.isTestFile(id))) { + this.changedTests.add(id) + return [id] + } return [] + } const files: string[] = []