/
file-watching.test.ts
148 lines (110 loc) · 4.82 KB
/
file-watching.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { afterEach, describe, expect, test } from 'vitest'
import { runVitestCli } from '../../test-utils'
const sourceFile = 'fixtures/math.ts'
const sourceFileContent = readFileSync(sourceFile, 'utf-8')
const testFile = 'fixtures/math.test.ts'
const testFileContent = readFileSync(testFile, 'utf-8')
const configFile = 'fixtures/vitest.config.ts'
const configFileContent = readFileSync(configFile, 'utf-8')
const forceTriggerFile = 'fixtures/force-watch/trigger.js'
const forceTriggerFileContent = readFileSync(forceTriggerFile, 'utf-8')
const cliArgs = ['--root', 'fixtures', '--watch']
const cleanups: (() => void)[] = []
function editFile(fileContent: string) {
return `// Modified by file-watching.test.ts
${fileContent}
console.log("New code running"); // This is used to check that edited changes are actually run, and cached files are not run instead
`
}
afterEach(() => {
writeFileSync(sourceFile, sourceFileContent, 'utf8')
writeFileSync(testFile, testFileContent, 'utf8')
writeFileSync(configFile, configFileContent, 'utf8')
writeFileSync(forceTriggerFile, forceTriggerFileContent, 'utf8')
cleanups.splice(0).forEach(cleanup => cleanup())
})
test('editing source file triggers re-run', async () => {
const vitest = await runVitestCli(...cliArgs)
writeFileSync(sourceFile, editFile(sourceFileContent), 'utf8')
await vitest.waitForStdout('New code running')
await vitest.waitForStdout('RERUN ../math.ts')
await vitest.waitForStdout('1 passed')
})
test('editing force rerun trigger reruns all tests', async () => {
const vitest = await runVitestCli(...cliArgs)
writeFileSync(forceTriggerFile, editFile(forceTriggerFileContent), 'utf8')
await vitest.waitForStdout('Waiting for file changes...')
await vitest.waitForStdout('RERUN ../force-watch/trigger.js')
await vitest.waitForStdout('example.test.ts')
await vitest.waitForStdout('math.test.ts')
await vitest.waitForStdout('2 passed')
})
test('editing test file triggers re-run', async () => {
const vitest = await runVitestCli(...cliArgs)
writeFileSync(testFile, editFile(testFileContent), 'utf8')
await vitest.waitForStdout('New code running')
await vitest.waitForStdout('RERUN ../math.test.ts')
await vitest.waitForStdout('1 passed')
})
test('editing config file triggers re-run', async () => {
const vitest = await runVitestCli(...cliArgs)
writeFileSync(configFile, editFile(configFileContent), 'utf8')
await vitest.waitForStdout('New code running')
await vitest.waitForStdout('Restarting due to config changes')
await vitest.waitForStdout('2 passed')
})
test('editing config file reloads new changes', async () => {
const vitest = await runVitestCli(...cliArgs)
writeFileSync(configFile, configFileContent.replace('reporters: \'verbose\'', 'reporters: \'tap\''), 'utf8')
await vitest.waitForStdout('TAP version')
await vitest.waitForStdout('ok 2')
})
test('adding a new test file triggers re-run', async () => {
const vitest = await runVitestCli(...cliArgs)
const testFile = 'fixtures/new-dynamic.test.ts'
const testFileContent = `
import { expect, test } from "vitest";
test("dynamic test case", () => {
console.log("Running added dynamic test")
expect(true).toBeTruthy()
})
`
cleanups.push(() => rmSync(testFile))
writeFileSync(testFile, testFileContent, 'utf-8')
await vitest.waitForStdout('Running added dynamic test')
await vitest.waitForStdout('RERUN ../new-dynamic.test.ts')
await vitest.waitForStdout('1 passed')
})
test('editing source file generates new test report to file system', async () => {
const report = 'fixtures/test-results/junit.xml'
if (existsSync(report))
rmSync(report)
// Test report should not be present before test run
expect(existsSync(report)).toBe(false)
const vitest = await runVitestCli(
...cliArgs,
'--reporter', 'verbose',
'--reporter', 'junit',
'--output-file', 'test-results/junit.xml',
)
// Test report should be generated on initial test run
expect(existsSync(report)).toBe(true)
// Test report should be re-generated on second test run
rmSync(report)
expect(existsSync(report)).toBe(false)
writeFileSync(sourceFile, editFile(sourceFileContent), 'utf8')
await vitest.waitForStdout('JUNIT report written')
await vitest.waitForStdout(report)
expect(existsSync(report)).toBe(true)
})
describe('browser', () => {
test.runIf((process.platform !== 'win32'))('editing source file triggers re-run', async () => {
const vitest = await runVitestCli(...cliArgs, '--browser.enabled', '--browser.headless', '--browser.name=chrome')
writeFileSync(sourceFile, editFile(sourceFileContent), 'utf8')
await vitest.waitForStdout('New code running')
await vitest.waitForStdout('RERUN ../math.ts')
await vitest.waitForStdout('1 passed')
vitest.write('q')
}, { retry: 3 })
})