Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vitest): correctly rerun test files on change if server was restarted #4871

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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