Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/vitest/src/node/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand All @@ -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
Expand Down
43 changes: 42 additions & 1 deletion test/watch/test/workspaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
})
1 change: 0 additions & 1 deletion test/workspaces/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export default defineConfig({
globalConfigValue: true,
},
projects: [

'space_2',
'./space_*/vitest.config.ts',
'./space_1/*.config.ts',
Expand Down