Skip to content

Commit

Permalink
fix: don't rerun more then needed in watch mode (fix #442) (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
Demivan committed Jan 22, 2022
1 parent 64f07e2 commit 4d9e7cc
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions packages/vitest/src/node/core.ts
Expand Up @@ -282,8 +282,8 @@ export class Vitest {
private registerWatcher() {
const onChange = (id: string) => {
id = slash(id)
this.handleFileChanged(id)
if (this.changedTests.size)
const needsRerun = this.handleFileChanged(id)
if (needsRerun)
this.scheduleRerun(id)
}
const onUnlink = (id: string) => {
Expand Down Expand Up @@ -315,25 +315,35 @@ export class Vitest {
}
}

private handleFileChanged(id: string) {
/**
* @returns A value indicating whether rerun is needed (changedTests was mutated)
*/
private handleFileChanged(id: string): boolean {
if (this.changedTests.has(id) || this.invalidates.has(id) || this.config.watchIgnore.some(i => id.match(i)))
return
return false

const mod = this.server.moduleGraph.getModuleById(id)
if (!mod)
return
return false

this.invalidates.add(id)

if (this.state.filesMap.has(id)) {
this.changedTests.add(id)
return
return true
}

let rerun = false
mod.importers.forEach((i) => {
if (i.id)
this.handleFileChanged(i.id)
if (!i.id)
return

const heedsRerun = this.handleFileChanged(i.id)
if (heedsRerun)
rerun = true
})

return rerun
}

async close() {
Expand Down

0 comments on commit 4d9e7cc

Please sign in to comment.