Skip to content

Commit

Permalink
fix(vitest): use file instead of id for HMR (#4938)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jan 11, 2024
1 parent 7fd44dc commit ca76f45
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { basename, dirname, join, normalize, relative, resolve } from 'pathe'
import fg from 'fast-glob'
import mm from 'micromatch'
import c from 'picocolors'
import { normalizeRequestId } from 'vite-node/utils'
import { ViteNodeRunner } from 'vite-node/client'
import { SnapshotManager } from '@vitest/snapshot/manager'
import type { CancelReason, File } from '@vitest/runner'
Expand Down Expand Up @@ -646,23 +645,23 @@ export class Vitest {
}, WATCHER_DEBOUNCE)
}

public getModuleProjects(id: string) {
public getModuleProjects(filepath: string) {
return this.projects.filter((project) => {
return project.getModulesByFilepath(id).size
return project.getModulesByFilepath(filepath).size
// TODO: reevaluate || project.browser?.moduleGraph.getModulesByFile(id)?.size
})
}

private unregisterWatcher = noop
private registerWatcher() {
const updateLastChanged = (id: string) => {
const projects = this.getModuleProjects(id)
const updateLastChanged = (filepath: string) => {
const projects = this.getModuleProjects(filepath)
projects.forEach(({ server, browser }) => {
const serverMods = server.moduleGraph.getModulesByFile(id)
const serverMods = server.moduleGraph.getModulesByFile(filepath)
serverMods?.forEach(mod => server.moduleGraph.invalidateModule(mod))

if (browser) {
const browserMods = browser.moduleGraph.getModulesByFile(id)
const browserMods = browser.moduleGraph.getModulesByFile(filepath)
browserMods?.forEach(mod => browser.moduleGraph.invalidateModule(mod))
}
})
Expand Down Expand Up @@ -725,62 +724,56 @@ export class Vitest {
/**
* @returns A value indicating whether rerun is needed (changedTests was mutated)
*/
private handleFileChanged(id: string): string[] {
if (this.changedTests.has(id) || this.invalidates.has(id))
private handleFileChanged(filepath: string): string[] {
if (this.changedTests.has(filepath) || this.invalidates.has(filepath))
return []

if (mm.isMatch(id, this.config.forceRerunTriggers)) {
if (mm.isMatch(filepath, this.config.forceRerunTriggers)) {
this.state.getFilepaths().forEach(file => this.changedTests.add(file))
return [id]
return [filepath]
}

const projects = this.getModuleProjects(id)
const projects = this.getModuleProjects(filepath)
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]
if (this.state.filesMap.has(filepath) || this.projects.some(project => project.isTestFile(filepath))) {
this.changedTests.add(filepath)
return [filepath]
}
return []
}

const files: string[] = []

for (const project of projects) {
const { server } = project
const mods = project.getModulesByFilepath(id)
const mods = project.getModulesByFilepath(filepath)
if (!mods.size)
continue

// remove queries from id
id = normalizeRequestId(id, server.config.base)

this.invalidates.add(id)
this.invalidates.add(filepath)

// one of test files that we already run, or one of test files that we can run
if (this.state.filesMap.has(id) || project.isTestFile(id)) {
this.changedTests.add(id)
files.push(id)
if (this.state.filesMap.has(filepath) || project.isTestFile(filepath)) {
this.changedTests.add(filepath)
files.push(filepath)
continue
}

let rerun = false
for (const mod of mods) {
if (!mod.id)
continue
mod.importers.forEach((i) => {
if (!i.id)
if (!i.file)
return

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

if (rerun)
files.push(id)
files.push(filepath)
}

return files
Expand Down

0 comments on commit ca76f45

Please sign in to comment.