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 find module if it has a version query #4976

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
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
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