Skip to content

Commit

Permalink
fix(vitest): correctly rerun test files on change if server was resta…
Browse files Browse the repository at this point in the history
…rted (#4871)
  • Loading branch information
sheremet-va committed Jan 4, 2024
1 parent e12a5a3 commit 6088b37
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
})
}
Expand Down Expand Up @@ -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[] = []

Expand Down

0 comments on commit 6088b37

Please sign in to comment.