1
1
import { 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'
4
4
import { dirname , resolve } from 'pathe'
5
5
import { startWatchMode } from './utils'
6
6
7
7
const file = fileURLToPath ( import . meta. url )
8
8
const dir = dirname ( file )
9
9
const root = resolve ( dir , '..' , '..' , 'workspaces' )
10
10
const config = resolve ( root , 'vitest.config.ts' )
11
+ const cleanups : ( ( ) => void ) [ ] = [ ]
11
12
12
13
const srcMathFile = resolve ( root , 'src' , 'math.ts' )
13
14
const specSpace2File = resolve ( root , 'space_2' , 'test' , 'node.spec.ts' )
14
15
15
16
const srcMathContent = readFileSync ( srcMathFile , 'utf-8' )
16
17
const specSpace2Content = readFileSync ( specSpace2File , 'utf-8' )
17
18
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
+
18
28
function startVitest ( ) {
19
29
return startWatchMode (
20
30
{ cwd : root , env : { TEST_WATCH : 'true' } } ,
@@ -27,6 +37,10 @@ function startVitest() {
27
37
)
28
38
}
29
39
40
+ afterEach ( ( ) => {
41
+ cleanups . splice ( 0 ) . forEach ( cleanup => cleanup ( ) )
42
+ } )
43
+
30
44
afterAll ( ( ) => {
31
45
writeFileSync ( srcMathFile , srcMathContent , 'utf8' )
32
46
writeFileSync ( specSpace2File , specSpace2Content , 'utf8' )
@@ -48,7 +62,7 @@ it('editing a file that is imported in different workspaces reruns both files',
48
62
writeFileSync ( srcMathFile , `${ srcMathContent } \n` , 'utf8' )
49
63
50
64
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' )
52
66
await vitest . waitForOutput ( '|space_1| test/math.spec.ts' )
53
67
await vitest . waitForOutput ( 'Test Files 2 passed' )
54
68
} )
@@ -65,3 +79,46 @@ it('filters by test name inside a workspace', async () => {
65
79
await vitest . waitForOutput ( 'Test name pattern: /2 x 2 = 4/' )
66
80
await vitest . waitForOutput ( 'Test Files 1 passed' )
67
81
} )
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