Skip to content

Commit 0f33506

Browse files
authored
fix: rerun tests when project's setup file is changed (#8097)
1 parent 7ddcd33 commit 0f33506

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

packages/vitest/src/node/watcher.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ export class VitestWatcher {
140140
}
141141
}
142142

143+
private handleSetupFile(filepath: string) {
144+
let isSetupFile: boolean = false
145+
146+
this.vitest.projects.forEach((project) => {
147+
if (!project.config.setupFiles.includes(filepath)) {
148+
return
149+
}
150+
151+
this.vitest.state.filesMap.forEach((files) => {
152+
files.forEach((file) => {
153+
if (file.projectName === project.name) {
154+
isSetupFile = true
155+
this.changedTests.add(file.filepath)
156+
}
157+
})
158+
})
159+
})
160+
161+
return isSetupFile
162+
}
163+
143164
/**
144165
* @returns A value indicating whether rerun is needed (changedTests was mutated)
145166
*/
@@ -153,6 +174,10 @@ export class VitestWatcher {
153174
return true
154175
}
155176

177+
if (this.handleSetupFile(filepath)) {
178+
return true
179+
}
180+
156181
const projects = this.vitest.projects.filter((project) => {
157182
const moduleGraph = project.browser?.vite.moduleGraph || project.vite.moduleGraph
158183
return moduleGraph.getModulesByFile(filepath)?.size

test/watch/test/workspaces.test.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { fileURLToPath } from 'node:url'
33
import { dirname, resolve } from 'pathe'
44
import { afterAll, afterEach, expect, it } from 'vitest'
55

6-
import { runVitestCli } from '../../test-utils'
6+
import { runInlineTests, runVitestCli } from '../../test-utils'
77

88
const file = fileURLToPath(import.meta.url)
99
const dir = dirname(file)
@@ -125,3 +125,44 @@ it('adding a new test file matching project specific config triggers re-run', as
125125
expect(vitest.stdout).not.include('|node|')
126126
expect(vitest.stdout).not.include('|happy-dom|')
127127
})
128+
129+
it('editing a setup file inside the project reruns tests', async () => {
130+
const { fs, vitest } = await runInlineTests({
131+
'setupFile.js': '',
132+
'project-1/basic.test.js': `test("[p1] reruns")`,
133+
'project-2/basic.test.js': `test("[p2] doesn\'t rerun")`,
134+
'vitest.config.js': {
135+
test: {
136+
projects: [
137+
{
138+
test: {
139+
name: 'p1',
140+
include: ['./project-1/basic.test.js'],
141+
setupFiles: ['./setupFile.js'],
142+
globals: true,
143+
},
144+
},
145+
{
146+
test: {
147+
name: 'p2',
148+
include: ['./project-2/basic.test.js'],
149+
globals: true,
150+
},
151+
},
152+
],
153+
},
154+
},
155+
}, { watch: true })
156+
157+
await vitest.waitForStdout('Waiting for file changes')
158+
expect(vitest.stdout).toContain('[p1] reruns')
159+
expect(vitest.stdout).toContain('[p2] doesn\'t rerun')
160+
161+
fs.editFile('./setupFile.js', () => '// ---edit')
162+
163+
vitest.resetOutput()
164+
await vitest.waitForStdout('Test Files 1 passed')
165+
166+
expect(vitest.stdout).toContain('[p1] reruns')
167+
expect(vitest.stdout).not.toContain('[p2] doesn\'t rerun')
168+
})

test/workspaces/vitest.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export default defineConfig({
2323
globalConfigValue: true,
2424
},
2525
projects: [
26-
2726
'space_2',
2827
'./space_*/vitest.config.ts',
2928
'./space_1/*.config.ts',

0 commit comments

Comments
 (0)