diff --git a/packages/vitest/src/node/watcher.ts b/packages/vitest/src/node/watcher.ts index 8e5219a7cf7d..d80c99a1b752 100644 --- a/packages/vitest/src/node/watcher.ts +++ b/packages/vitest/src/node/watcher.ts @@ -140,6 +140,27 @@ export class VitestWatcher { } } + private handleSetupFile(filepath: string) { + let isSetupFile: boolean = false + + this.vitest.projects.forEach((project) => { + if (!project.config.setupFiles.includes(filepath)) { + return + } + + this.vitest.state.filesMap.forEach((files) => { + files.forEach((file) => { + if (file.projectName === project.name) { + isSetupFile = true + this.changedTests.add(file.filepath) + } + }) + }) + }) + + return isSetupFile + } + /** * @returns A value indicating whether rerun is needed (changedTests was mutated) */ @@ -153,6 +174,10 @@ export class VitestWatcher { return true } + if (this.handleSetupFile(filepath)) { + return true + } + const projects = this.vitest.projects.filter((project) => { const moduleGraph = project.browser?.vite.moduleGraph || project.vite.moduleGraph return moduleGraph.getModulesByFile(filepath)?.size diff --git a/test/watch/test/workspaces.test.ts b/test/watch/test/workspaces.test.ts index 6e202e4c6eb8..52c416cb5c9d 100644 --- a/test/watch/test/workspaces.test.ts +++ b/test/watch/test/workspaces.test.ts @@ -3,7 +3,7 @@ import { fileURLToPath } from 'node:url' import { dirname, resolve } from 'pathe' import { afterAll, afterEach, expect, it } from 'vitest' -import { runVitestCli } from '../../test-utils' +import { runInlineTests, runVitestCli } from '../../test-utils' const file = fileURLToPath(import.meta.url) const dir = dirname(file) @@ -125,3 +125,44 @@ it('adding a new test file matching project specific config triggers re-run', as expect(vitest.stdout).not.include('|node|') expect(vitest.stdout).not.include('|happy-dom|') }) + +it('editing a setup file inside the project reruns tests', async () => { + const { fs, vitest } = await runInlineTests({ + 'setupFile.js': '', + 'project-1/basic.test.js': `test("[p1] reruns")`, + 'project-2/basic.test.js': `test("[p2] doesn\'t rerun")`, + 'vitest.config.js': { + test: { + projects: [ + { + test: { + name: 'p1', + include: ['./project-1/basic.test.js'], + setupFiles: ['./setupFile.js'], + globals: true, + }, + }, + { + test: { + name: 'p2', + include: ['./project-2/basic.test.js'], + globals: true, + }, + }, + ], + }, + }, + }, { watch: true }) + + await vitest.waitForStdout('Waiting for file changes') + expect(vitest.stdout).toContain('[p1] reruns') + expect(vitest.stdout).toContain('[p2] doesn\'t rerun') + + fs.editFile('./setupFile.js', () => '// ---edit') + + vitest.resetOutput() + await vitest.waitForStdout('Test Files 1 passed') + + expect(vitest.stdout).toContain('[p1] reruns') + expect(vitest.stdout).not.toContain('[p2] doesn\'t rerun') +}) diff --git a/test/workspaces/vitest.config.ts b/test/workspaces/vitest.config.ts index 953ea057fa19..7a885957313e 100644 --- a/test/workspaces/vitest.config.ts +++ b/test/workspaces/vitest.config.ts @@ -23,7 +23,6 @@ export default defineConfig({ globalConfigValue: true, }, projects: [ - 'space_2', './space_*/vitest.config.ts', './space_1/*.config.ts',