Skip to content

Commit a084cea

Browse files
authored
fix(vitest): rerun tests if a file loaded with query changes (#4130)
1 parent 90ff899 commit a084cea

File tree

5 files changed

+50
-27
lines changed

5 files changed

+50
-27
lines changed

packages/vitest/src/node/core.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ export class Vitest {
586586

587587
public getModuleProjects(id: string) {
588588
return this.projects.filter((project) => {
589-
return project.getModuleById(id)
589+
return project.getModulesByFilepath(id).size
590590
// TODO: reevaluate || project.browser?.moduleGraph.getModulesByFile(id)?.size
591591
})
592592
}
@@ -596,9 +596,13 @@ export class Vitest {
596596
const updateLastChanged = (id: string) => {
597597
const projects = this.getModuleProjects(id)
598598
projects.forEach(({ server, browser }) => {
599-
const mod = server.moduleGraph.getModuleById(id) || browser?.moduleGraph.getModuleById(id)
600-
if (mod)
601-
server.moduleGraph.invalidateModule(mod)
599+
const serverMods = server.moduleGraph.getModulesByFile(id)
600+
serverMods?.forEach(mod => server.moduleGraph.invalidateModule(mod))
601+
602+
if (browser) {
603+
const browserMods = browser.moduleGraph.getModulesByFile(id)
604+
browserMods?.forEach(mod => browser.moduleGraph.invalidateModule(mod))
605+
}
602606
})
603607
}
604608

@@ -675,22 +679,10 @@ export class Vitest {
675679
const files: string[] = []
676680

677681
for (const project of projects) {
678-
const { server, browser } = project
679-
const mod = server.moduleGraph.getModuleById(id) || browser?.moduleGraph.getModuleById(id)
680-
if (!mod) {
681-
// files with `?v=` query from the browser
682-
const mods = browser?.moduleGraph.getModulesByFile(id)
683-
if (!mods?.size)
684-
return []
685-
let rerun = false
686-
mods.forEach((m) => {
687-
if (m.id && this.handleFileChanged(m.id))
688-
rerun = true
689-
})
690-
if (rerun)
691-
files.push(id)
682+
const { server } = project
683+
const mods = project.getModulesByFilepath(id)
684+
if (!mods.size)
692685
continue
693-
}
694686

695687
// remove queries from id
696688
id = normalizeRequestId(id, server.config.base)
@@ -705,14 +697,18 @@ export class Vitest {
705697
}
706698

707699
let rerun = false
708-
mod.importers.forEach((i) => {
709-
if (!i.id)
710-
return
711-
712-
const heedsRerun = this.handleFileChanged(i.id)
713-
if (heedsRerun)
714-
rerun = true
715-
})
700+
for (const mod of mods) {
701+
if (!mod.id)
702+
continue
703+
mod.importers.forEach((i) => {
704+
if (!i.id)
705+
return
706+
707+
const heedsRerun = this.handleFileChanged(i.id)
708+
if (heedsRerun)
709+
rerun = true
710+
})
711+
}
716712

717713
if (rerun)
718714
files.push(id)

packages/vitest/src/node/workspace.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ export class WorkspaceProject {
7777
return this.ctx.getCoreWorkspaceProject() === this
7878
}
7979

80+
// it's possible that file path was imported with different queries (?raw, ?url, etc)
81+
getModulesByFilepath(file: string) {
82+
const set = this.server.moduleGraph.getModulesByFile(file)
83+
|| this.browser?.moduleGraph.getModulesByFile(file)
84+
return set || new Set()
85+
}
86+
8087
getModuleById(id: string) {
8188
return this.server.moduleGraph.getModuleById(id)
8289
|| this.browser?.moduleGraph.getModuleById(id)

test/watch/fixtures/42.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

test/watch/fixtures/example.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import { expect, test } from 'vitest'
22

33
import { getHelloWorld } from './example'
44

5+
// @ts-expect-error not typed txt
6+
import answer from './42.txt?raw'
7+
8+
test('answer is 42', () => {
9+
expect(answer).toContain('42')
10+
})
11+
512
test('getHello', async () => {
613
expect(getHelloWorld()).toBe('Hello world')
714
})

test/watch/test/file-watching.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ test('editing source file triggers re-run', async () => {
5050
await vitest.waitForStdout('1 passed')
5151
})
5252

53+
test('editing file that was imported with a query reruns suite', async () => {
54+
const vitest = await runVitestCli(...cliArgs)
55+
56+
testUtils.editFile(
57+
testUtils.resolvePath(import.meta.url, '../fixtures/42.txt'),
58+
file => `${file}\n`,
59+
)
60+
61+
await vitest.waitForStdout('RERUN ../42.txt')
62+
await vitest.waitForStdout('1 passed')
63+
})
64+
5365
test('editing force rerun trigger reruns all tests', async () => {
5466
const vitest = await runVitestCli(...cliArgs)
5567

0 commit comments

Comments
 (0)