11import { fileURLToPath } from 'node:url'
2- import { readFileSync , writeFileSync } from 'node:fs'
3- import { afterAll , it } from 'vitest'
2+ import { readFileSync , rmSync , writeFileSync } from 'node:fs'
3+ import { afterAll , afterEach , expect , it } from 'vitest'
44import { dirname , resolve } from 'pathe'
55import { startWatchMode } from './utils'
66
77const file = fileURLToPath ( import . meta. url )
88const dir = dirname ( file )
99const root = resolve ( dir , '..' , '..' , 'workspaces' )
1010const config = resolve ( root , 'vitest.config.ts' )
11+ const cleanups : ( ( ) => void ) [ ] = [ ]
1112
1213const srcMathFile = resolve ( root , 'src' , 'math.ts' )
1314const specSpace2File = resolve ( root , 'space_2' , 'test' , 'node.spec.ts' )
1415
1516const srcMathContent = readFileSync ( srcMathFile , 'utf-8' )
1617const specSpace2Content = readFileSync ( specSpace2File , 'utf-8' )
1718
19+ const dynamicTestContent = `// Dynamic test added by test/watch/test/workspaces.test.ts
20+ import { expect, test } from "vitest";
21+
22+ test("dynamic test case", () => {
23+ console.log("Running added dynamic test")
24+ expect(true).toBeTruthy()
25+ })
26+ `
27+
1828function startVitest ( ) {
1929 return startWatchMode (
2030 { cwd : root , env : { TEST_WATCH : 'true' } } ,
@@ -27,6 +37,10 @@ function startVitest() {
2737 )
2838}
2939
40+ afterEach ( ( ) => {
41+ cleanups . splice ( 0 ) . forEach ( cleanup => cleanup ( ) )
42+ } )
43+
3044afterAll ( ( ) => {
3145 writeFileSync ( srcMathFile , srcMathContent , 'utf8' )
3246 writeFileSync ( specSpace2File , specSpace2Content , 'utf8' )
@@ -48,7 +62,7 @@ it('editing a file that is imported in different workspaces reruns both files',
4862 writeFileSync ( srcMathFile , `${ srcMathContent } \n` , 'utf8' )
4963
5064 await vitest . waitForOutput ( 'RERUN src/math.ts' )
51- await vitest . waitForOutput ( '|space_3| math.space-test.ts' )
65+ await vitest . waitForOutput ( '|space_3| math.space-3- test.ts' )
5266 await vitest . waitForOutput ( '|space_1| test/math.spec.ts' )
5367 await vitest . waitForOutput ( 'Test Files 2 passed' )
5468} )
@@ -65,3 +79,46 @@ it('filters by test name inside a workspace', async () => {
6579 await vitest . waitForOutput ( 'Test name pattern: /2 x 2 = 4/' )
6680 await vitest . waitForOutput ( 'Test Files 1 passed' )
6781} )
82+
83+ it ( 'adding a new test file matching core project config triggers re-run' , async ( ) => {
84+ const vitest = await startVitest ( )
85+
86+ const testFile = resolve ( root , 'space_2' , 'test' , 'new-dynamic.test.ts' )
87+
88+ cleanups . push ( ( ) => rmSync ( testFile ) )
89+ writeFileSync ( testFile , dynamicTestContent , 'utf-8' )
90+
91+ await vitest . waitForOutput ( 'Running added dynamic test' )
92+ await vitest . waitForOutput ( 'RERUN space_2/test/new-dynamic.test.ts' )
93+ await vitest . waitForOutput ( '|space_2| test/new-dynamic.test.ts' )
94+
95+ // Wait for tests to end
96+ await vitest . waitForOutput ( 'Waiting for file changes' )
97+
98+ // Test case should not be run by other projects
99+ expect ( vitest . output ) . not . include ( '|space_1|' )
100+ expect ( vitest . output ) . not . include ( '|space_3|' )
101+ expect ( vitest . output ) . not . include ( '|node|' )
102+ expect ( vitest . output ) . not . include ( '|happy-dom|' )
103+ } )
104+
105+ it ( 'adding a new test file matching project specific config triggers re-run' , async ( ) => {
106+ const vitest = await startVitest ( )
107+
108+ const testFile = resolve ( root , 'space_3' , 'new-dynamic.space-3-test.ts' )
109+ cleanups . push ( ( ) => rmSync ( testFile ) )
110+ writeFileSync ( testFile , dynamicTestContent , 'utf-8' )
111+
112+ await vitest . waitForOutput ( 'Running added dynamic test' )
113+ await vitest . waitForOutput ( 'RERUN space_3/new-dynamic.space-3-test.ts' )
114+ await vitest . waitForOutput ( '|space_3| new-dynamic.space-3-test.ts' )
115+
116+ // Wait for tests to end
117+ await vitest . waitForOutput ( 'Waiting for file changes' )
118+
119+ // Test case should not be run by other projects
120+ expect ( vitest . output ) . not . include ( '|space_1|' )
121+ expect ( vitest . output ) . not . include ( '|space_2|' )
122+ expect ( vitest . output ) . not . include ( '|node|' )
123+ expect ( vitest . output ) . not . include ( '|happy-dom|' )
124+ } )
0 commit comments