Skip to content

Commit 952c31d

Browse files
authoredJan 16, 2024
fix(vitest): correctly find module if it has a version query (#4976)
1 parent df0db6a commit 952c31d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
 

‎packages/vite-node/src/server.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ export class ViteNodeServer {
195195
return 'web'
196196
}
197197

198+
private getChangedModule(
199+
id: string,
200+
file: string,
201+
) {
202+
const module = this.server.moduleGraph.getModuleById(id) || this.server.moduleGraph.getModuleById(file)
203+
if (module)
204+
return module
205+
const _modules = this.server.moduleGraph.getModulesByFile(file)
206+
if (!_modules || !_modules.size)
207+
return null
208+
// find the latest changed module
209+
const modules = [..._modules]
210+
let mod = modules[0]
211+
let latestMax = -1
212+
for (const m of _modules) {
213+
const timestamp = Math.max(m.lastHMRTimestamp, m.lastInvalidationTimestamp)
214+
if (timestamp > latestMax) {
215+
latestMax = timestamp
216+
mod = m
217+
}
218+
}
219+
return mod
220+
}
221+
198222
private async _fetchModule(id: string, transformMode: 'web' | 'ssr'): Promise<FetchResult> {
199223
let result: FetchResult
200224

@@ -212,7 +236,7 @@ export class ViteNodeServer {
212236

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

215-
const moduleNode = this.server.moduleGraph.getModuleById(id) || this.server.moduleGraph.getModuleById(filePath)
239+
const moduleNode = this.getChangedModule(id, filePath)
216240
const cache = this.fetchCaches[transformMode].get(filePath)
217241

218242
// lastUpdateTimestamp is the timestamp that marks the last time the module was changed

‎packages/vitest/src/node/core.ts

+6
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,12 @@ export class Vitest {
705705
this.changedTests.add(id)
706706
this.scheduleRerun([id])
707707
}
708+
else {
709+
// it's possible that file was already there but watcher triggered "add" event instead
710+
const needsRerun = this.handleFileChanged(id)
711+
if (needsRerun.length)
712+
this.scheduleRerun(needsRerun)
713+
}
708714
}
709715
const watcher = this.server.watcher
710716

0 commit comments

Comments
 (0)
Please sign in to comment.