Skip to content

Commit 6088b37

Browse files
authoredJan 4, 2024
fix(vitest): correctly rerun test files on change if server was restarted (#4871)
1 parent e12a5a3 commit 6088b37

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed
 

‎packages/vitest/src/node/core.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ export class Vitest {
126126
const serverRestart = server.restart
127127
server.restart = async (...args) => {
128128
await Promise.all(this._onRestartListeners.map(fn => fn()))
129-
return await serverRestart(...args)
129+
await serverRestart(...args)
130+
// watcher is recreated on restart
131+
this.unregisterWatcher()
132+
this.registerWatcher()
130133
}
131134

132135
// since we set `server.hmr: false`, Vite does not auto restart itself
@@ -136,6 +139,9 @@ export class Vitest {
136139
if (isConfig) {
137140
await Promise.all(this._onRestartListeners.map(fn => fn('config')))
138141
await serverRestart()
142+
// watcher is recreated on restart
143+
this.unregisterWatcher()
144+
this.registerWatcher()
139145
}
140146
})
141147
}
@@ -729,8 +735,15 @@ export class Vitest {
729735
}
730736

731737
const projects = this.getModuleProjects(id)
732-
if (!projects.length)
738+
if (!projects.length) {
739+
// if there are no modules it's possible that server was restarted
740+
// we don't have information about importers anymore, so let's check if the file is a test file at least
741+
if (this.state.filesMap.has(id) || this.projects.some(project => project.isTestFile(id))) {
742+
this.changedTests.add(id)
743+
return [id]
744+
}
733745
return []
746+
}
734747

735748
const files: string[] = []
736749

0 commit comments

Comments
 (0)
Please sign in to comment.