/
file-watching.test.ts
102 lines (75 loc) · 3.21 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
import { readFileSync, rmSync, writeFileSync } from 'node:fs'
import { afterEach, describe, test } from 'vitest'
import { startWatchMode } from './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 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')
cleanups.splice(0).forEach(cleanup => cleanup())
})
test('editing source file triggers re-run', async () => {
const vitest = await startWatchMode()
writeFileSync(sourceFile, editFile(sourceFileContent), 'utf8')
await vitest.waitForOutput('New code running')
await vitest.waitForOutput('RERUN ../math.ts')
await vitest.waitForOutput('1 passed')
})
test('editing test file triggers re-run', async () => {
const vitest = await startWatchMode()
writeFileSync(testFile, editFile(testFileContent), 'utf8')
await vitest.waitForOutput('New code running')
await vitest.waitForOutput('RERUN ../math.test.ts')
await vitest.waitForOutput('1 passed')
})
test('editing config file triggers re-run', async () => {
const vitest = await startWatchMode()
writeFileSync(configFile, editFile(configFileContent), 'utf8')
await vitest.waitForOutput('New code running')
await vitest.waitForOutput('Restarting due to config changes')
await vitest.waitForOutput('2 passed')
})
test('editing config file reloads new changes', async () => {
const vitest = await startWatchMode()
writeFileSync(configFile, configFileContent.replace('reporters: \'verbose\'', 'reporters: \'tap\''), 'utf8')
await vitest.waitForOutput('TAP version')
await vitest.waitForOutput('ok 2')
})
test('adding a new test file triggers re-run', async () => {
const vitest = await startWatchMode()
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.waitForOutput('Running added dynamic test')
await vitest.waitForOutput('RERUN ../new-dynamic.test.ts')
await vitest.waitForOutput('1 passed')
})
describe('browser', () => {
test.runIf((process.platform !== 'win32'))('editing source file triggers re-run', async () => {
const vitest = await startWatchMode('--browser.enabled', '--browser.headless', '--browser.name=chrome')
writeFileSync(sourceFile, editFile(sourceFileContent), 'utf8')
await vitest.waitForOutput('New code running')
await vitest.waitForOutput('RERUN ../math.ts')
await vitest.waitForOutput('1 passed')
vitest.write('q')
})
})