Skip to content

Commit

Permalink
fix(vitest): correctly find module if it has a version query (#4976)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Jan 16, 2024
1 parent df0db6a commit 952c31d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
26 changes: 25 additions & 1 deletion packages/vite-node/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,30 @@ export class ViteNodeServer {
return 'web'
}

private getChangedModule(
id: string,
file: string,
) {
const module = this.server.moduleGraph.getModuleById(id) || this.server.moduleGraph.getModuleById(file)
if (module)
return module
const _modules = this.server.moduleGraph.getModulesByFile(file)
if (!_modules || !_modules.size)
return null
// find the latest changed module
const modules = [..._modules]
let mod = modules[0]
let latestMax = -1
for (const m of _modules) {
const timestamp = Math.max(m.lastHMRTimestamp, m.lastInvalidationTimestamp)
if (timestamp > latestMax) {
latestMax = timestamp
mod = m
}
}
return mod
}

private async _fetchModule(id: string, transformMode: 'web' | 'ssr'): Promise<FetchResult> {
let result: FetchResult

Expand All @@ -212,7 +236,7 @@ export class ViteNodeServer {

const { path: filePath } = toFilePath(id, this.server.config.root)

const moduleNode = this.server.moduleGraph.getModuleById(id) || this.server.moduleGraph.getModuleById(filePath)
const moduleNode = this.getChangedModule(id, filePath)
const cache = this.fetchCaches[transformMode].get(filePath)

// lastUpdateTimestamp is the timestamp that marks the last time the module was changed
Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/node/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,12 @@ export class Vitest {
this.changedTests.add(id)
this.scheduleRerun([id])
}
else {
// it's possible that file was already there but watcher triggered "add" event instead
const needsRerun = this.handleFileChanged(id)
if (needsRerun.length)
this.scheduleRerun(needsRerun)
}
}
const watcher = this.server.watcher

Expand Down

0 comments on commit 952c31d

Please sign in to comment.