···22import { defineConfig } from 'vite'
33import { defaultExclude } from 'vitest/config'
4455-// Tests that mutate shared fixtures (editing files in `fixtures/watch`, driving
66-// git `--changed`) and cannot tolerate other tests mutating the working tree
77-// concurrently: an instance watching `fixtures/watch` restarts when a parallel
88-// test rewrites the fixture config and goes deaf to further file edits. Run in
99-// a serial project.
55+// Tests that drive git `--changed` against committed fixtures and cannot
66+// tolerate other tests mutating the working tree concurrently. Run in a serial
77+// project. Tests that only need a watched directory use `runInlineTests`
88+// instead: a private tmp root needs no serialization, but it cannot back the
99+// git-driven tests, which require tracked files.
1010const serialTests = [
1111 'test/git-changed.test.ts',
1212 'test/list-changed.test.ts',
1313 'test/setup-files.test.ts',
1414- 'test/watch/file-watching.test.ts',
1515- 'test/watch/global-setup-rerun.test.ts',
1614 'test/watch/related.test.ts',
1717- 'test/watch/restart-coalescing.test.ts',
1818- 'test/watch/stdout.test.ts',
1915]
20162117export default defineConfig({
+7
test/e2e/fixtures/related/math.test.ts
···11+import { expect, test } from 'vitest'
22+33+import { sum } from './math'
44+55+test('sum', () => {
66+ expect(sum(1, 2)).toBe(3)
77+})
+3
test/e2e/fixtures/related/math.ts
···11+export function sum(a: number, b: number) {
22+ return a + b
33+}
···11-import { expect, test } from 'vitest'
22-33-import { sum } from './math'
44-55-test('sum', () => {
66- expect(sum(1, 2)).toBe(3)
77-})
-3
test/e2e/fixtures/watch/math.ts
···11-export function sum(a: number, b: number) {
22- return a + b
33-}
-22
test/e2e/fixtures/watch/vitest.config.ts
···11-import { defaultExclude, defineConfig } from 'vitest/config'
22-33-// Patch stdin on the process so that we can fake it to seem like a real interactive terminal and pass the TTY checks
44-process.stdin.isTTY = true
55-process.stdin.setRawMode = () => process.stdin
66-77-export default defineConfig({
88- test: {
99- watch: true,
1010- exclude: [
1111- ...defaultExclude,
1212- '**/single-failed/**',
1313- ],
1414-1515- // This configuration is edited by tests
1616- reporters: 'verbose',
1717-1818- forceRerunTriggers: [
1919- '**/force-watch/**',
2020- ],
2121- },
2222-})
···22import { resolve } from 'pathe'
33import { test } from 'vitest'
4455+// `changed: true` is git-driven, so unlike the other watch tests this one has
66+// to run against a committed fixture: an inline tmp directory is either
77+// untracked (git reports every file as changed) or gitignored (git never
88+// reports its files as changed, even after edits).
59test('when nothing is changed, run nothing but keep watching', async () => {
610 const { vitest } = await runVitest({
77- root: 'fixtures/watch',
1111+ root: 'fixtures/related',
812 watch: true,
913 changed: true,
1014 })
···1216 await vitest.waitForStdout('No affected test files found')
1317 await vitest.waitForStdout('Waiting for file changes...')
14181515- editFile(resolve(import.meta.dirname, '../../fixtures/watch/math.ts'), content => `${content}\n\n`)
1919+ editFile(resolve(import.meta.dirname, '../../fixtures/related/math.ts'), content => `${content}\n\n`)
16201721 await vitest.waitForStdout('RERUN ../../math.ts')
1822 await vitest.waitForStdout('1 passed')
19232020- editFile(resolve(import.meta.dirname, '../../fixtures/watch/math.test.ts'), content => `${content}\n\n`)
2424+ editFile(resolve(import.meta.dirname, '../../fixtures/related/math.test.ts'), content => `${content}\n\n`)
21252226 await vitest.waitForStdout('RERUN ../../math.test.ts')
2327 await vitest.waitForStdout('1 passed')
+10-5
test/e2e/test/watch/restart-coalescing.test.ts
···11-import { runVitest } from '#test-utils'
11+import { runInlineTests } from '#test-utils'
22import { expect, test } from 'vitest'
3344// chokidar regularly delivers several change events for one config edit, each
···77// were re-instantiated but not yet initialized, crashing the run with
88// "Cannot read properties of undefined (reading 'logger')".
99test('concurrent restarts are coalesced instead of overlapping', async () => {
1010- const { ctx, vitest } = await runVitest({
1111- root: 'fixtures/watch',
1212- watch: true,
1313- })
1010+ const { ctx, vitest } = await runInlineTests({
1111+ 'basic.test.ts': /* ts */ `
1212+import { expect, test } from 'vitest'
1313+1414+test('basic', () => {
1515+ expect(1).toBe(1)
1616+})
1717+`,
1818+ }, { watch: true })
14191520 const restart = (ctx as any)._restart.bind(ctx)
1621 await Promise.all([restart('config'), restart('config'), restart('config')])
+19-13
test/e2e/test/watch/stdout.test.ts
···11-import { readFileSync, statSync, writeFileSync } from 'node:fs'
22-import { restoreFile, runVitest } from '#test-utils'
33-import { afterEach, test } from 'vitest'
44-55-const testFile = 'fixtures/watch/math.test.ts'
66-const testFileContent = readFileSync(testFile, 'utf-8')
77-const testFileStat = statSync(testFile)
88-99-afterEach(() => {
1010- restoreFile(testFile, testFileContent, testFileStat)
1111-})
11+import { runInlineTests } from '#test-utils'
22+import { test } from 'vitest'
123134test('console.log is visible on test re-run', async () => {
1414- const { vitest } = await runVitest({ root: 'fixtures/watch', watch: true })
55+ const { vitest, fs } = await runInlineTests({
66+ 'math.ts': /* ts */ `
77+export function sum(a: number, b: number) {
88+ return a + b
99+}
1010+`,
1111+ 'math.test.ts': /* ts */ `
1212+import { expect, test } from 'vitest'
1313+1414+import { sum } from './math'
1515+1616+test('sum', () => {
1717+ expect(sum(1, 2)).toBe(3)
1818+})
1919+`,
2020+ }, { watch: true })
15211622 const testCase = `
1723test('test with logging', () => {
···2228})
2329`
24302525- writeFileSync(testFile, `${testFileContent}${testCase}`, 'utf8')
3131+ fs.editFile('math.test.ts', content => `${content}${testCase}`)
26322733 await vitest.waitForStdout('stdout | math.test.ts > test with logging')
2834 await vitest.waitForStdout('First')