1
1
import { readFileSync , writeFileSync } from 'fs'
2
- import { afterEach , expect , test } from 'vitest'
2
+ import { afterEach , describe , test } from 'vitest'
3
3
4
- import { startWatchMode , waitFor } from './utils'
5
-
6
- const EDIT_COMMENT = '// Modified by file-watching.test.ts\n\n'
4
+ import { startWatchMode } from './utils'
7
5
8
6
const sourceFile = 'fixtures/math.ts'
9
7
const sourceFileContent = readFileSync ( sourceFile , 'utf-8' )
@@ -14,6 +12,13 @@ const testFileContent = readFileSync(testFile, 'utf-8')
14
12
const configFile = 'fixtures/vitest.config.ts'
15
13
const configFileContent = readFileSync ( configFile , 'utf-8' )
16
14
15
+ function editFile ( fileContent : string ) {
16
+ return `// Modified by file-watching.test.ts
17
+ ${ fileContent }
18
+ console.log("New code running"); // This is used to check that edited changes are actually run, and cached files are not run instead
19
+ `
20
+ }
21
+
17
22
afterEach ( ( ) => {
18
23
writeFileSync ( sourceFile , sourceFileContent , 'utf8' )
19
24
writeFileSync ( testFile , testFileContent , 'utf8' )
@@ -23,43 +28,52 @@ afterEach(() => {
23
28
test ( 'editing source file triggers re-run' , async ( ) => {
24
29
const vitest = await startWatchMode ( )
25
30
26
- writeFileSync ( sourceFile , ` ${ EDIT_COMMENT } ${ sourceFileContent } ` , 'utf8' )
31
+ writeFileSync ( sourceFile , editFile ( sourceFileContent ) , 'utf8' )
27
32
28
- await waitFor ( ( ) => {
29
- expect ( vitest . getOutput ( ) ) . toContain ( 'RERUN math.ts' )
30
- expect ( vitest . getOutput ( ) ) . toContain ( '1 passed' )
31
- } )
33
+ await vitest . waitForOutput ( 'New code running' )
34
+ await vitest . waitForOutput ( 'RERUN math.ts' )
35
+ await vitest . waitForOutput ( '1 passed' )
32
36
} )
33
37
34
38
test ( 'editing test file triggers re-run' , async ( ) => {
35
39
const vitest = await startWatchMode ( )
36
40
37
- writeFileSync ( testFile , ` ${ EDIT_COMMENT } ${ testFileContent } ` , 'utf8' )
41
+ writeFileSync ( testFile , editFile ( testFileContent ) , 'utf8' )
38
42
39
- await waitFor ( ( ) => {
40
- expect ( vitest . getOutput ( ) ) . toMatch ( 'RERUN math.test.ts' )
41
- expect ( vitest . getOutput ( ) ) . toMatch ( '1 passed' )
42
- } )
43
+ await vitest . waitForOutput ( 'New code running' )
44
+ await vitest . waitForOutput ( 'RERUN math.test.ts' )
45
+ await vitest . waitForOutput ( '1 passed' )
43
46
} )
44
47
45
48
test ( 'editing config file triggers re-run' , async ( ) => {
46
49
const vitest = await startWatchMode ( )
47
50
48
- writeFileSync ( configFile , ` ${ EDIT_COMMENT } ${ configFileContent } ` , 'utf8' )
51
+ writeFileSync ( configFile , editFile ( configFileContent ) , 'utf8' )
49
52
50
- await waitFor ( ( ) => {
51
- expect ( vitest . getOutput ( ) ) . toMatch ( 'Restarting due to config changes' )
52
- expect ( vitest . getOutput ( ) ) . toMatch ( '2 passed' )
53
- } )
53
+ await vitest . waitForOutput ( 'New code running' )
54
+ await vitest . waitForOutput ( 'Restarting due to config changes' )
55
+ await vitest . waitForOutput ( '2 passed' )
54
56
} )
55
57
56
58
test ( 'editing config file reloads new changes' , async ( ) => {
57
59
const vitest = await startWatchMode ( )
58
60
59
61
writeFileSync ( configFile , configFileContent . replace ( 'reporters: \'verbose\'' , 'reporters: \'tap\'' ) , 'utf8' )
60
62
61
- await waitFor ( ( ) => {
62
- expect ( vitest . getOutput ( ) ) . toMatch ( 'TAP version' )
63
- expect ( vitest . getOutput ( ) ) . toMatch ( 'ok 2' )
63
+ await vitest . waitForOutput ( 'TAP version' )
64
+ await vitest . waitForOutput ( 'ok 2' )
65
+ } )
66
+
67
+ describe ( 'browser' , ( ) => {
68
+ test . runIf ( ( process . platform !== 'win32' ) ) ( 'editing source file triggers re-run' , async ( ) => {
69
+ const vitest = await startWatchMode ( '--browser.enabled' , '--browser.headless' , '--browser.name=chrome' )
70
+
71
+ writeFileSync ( sourceFile , editFile ( sourceFileContent ) , 'utf8' )
72
+
73
+ await vitest . waitForOutput ( 'New code running' )
74
+ await vitest . waitForOutput ( 'RERUN math.ts' )
75
+ await vitest . waitForOutput ( '1 passed' )
76
+
77
+ vitest . write ( 'q' )
64
78
} )
65
79
} )
0 commit comments