/
index.ts
234 lines (197 loc) · 6.25 KB
/
index.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { Console } from 'node:console'
import { Writable } from 'node:stream'
import fs from 'node:fs'
import { type UserConfig, type VitestRunMode, afterEach } from 'vitest'
import type { Vitest } from 'vitest/node'
import { startVitest } from 'vitest/node'
import { type Options, execa } from 'execa'
import stripAnsi from 'strip-ansi'
export async function runVitest(config: UserConfig, cliFilters: string[] = [], mode: VitestRunMode = 'test') {
// Reset possible previous runs
process.exitCode = 0
// Prevent possible process.exit() calls, e.g. from --browser
const exit = process.exit
process.exit = (() => { }) as never
const { getLogs, restore } = captureLogs()
let vitest: Vitest | undefined
try {
vitest = await startVitest(mode, cliFilters, {
watch: false,
reporters: ['verbose'],
...config,
})
}
catch (e: any) {
return {
stderr: `${getLogs().stderr}\n${e.message}`,
stdout: getLogs().stdout,
exitCode: process.exitCode,
vitest,
}
}
finally {
restore()
}
const exitCode = process.exitCode
process.exitCode = 0
process.exit = exit
return { ...getLogs(), exitCode, vitest }
}
function captureLogs() {
const stdout: string[] = []
const stderr: string[] = []
const streams = {
stdout: new Writable({
write(chunk, _, callback) {
stdout.push(chunk.toString())
callback()
},
}),
stderr: new Writable({
write(chunk, _, callback) {
stderr.push(chunk.toString())
callback()
},
}),
}
const originalConsole = globalThis.console
globalThis.console = new Console(streams)
const originalStdoutWrite = process.stdout.write
process.stdout.write = streams.stdout.write.bind(streams.stdout) as any
const originalStderrWrite = process.stderr.write
process.stderr.write = streams.stderr.write.bind(streams.stderr) as any
return {
restore: () => {
globalThis.console = originalConsole
process.stdout.write = originalStdoutWrite
process.stderr.write = originalStderrWrite
},
getLogs() {
return {
stdout: stripAnsi(stdout.join('')),
stderr: stripAnsi(stderr.join('')),
}
},
}
}
export async function runCli(command: string, _options?: Options | string, ...args: string[]) {
let options = _options
if (typeof _options === 'string') {
args.unshift(_options)
options = undefined
}
const subprocess = execa(command, args, options as Options)
let setDone: (value?: unknown) => void
const isDone = new Promise(resolve => (setDone = resolve))
const cli = {
stdout: '',
stderr: '',
stdoutListeners: [] as (() => void)[],
stderrListeners: [] as (() => void)[],
isDone,
write(text: string) {
this.resetOutput()
subprocess.stdin!.write(text)
},
waitForStdout(expected: string) {
const error = new Error('Timeout error')
Error.captureStackTrace(error, this.waitForStdout)
return new Promise<void>((resolve, reject) => {
if (this.stdout.includes(expected))
return resolve()
const timeout = setTimeout(() => {
error.message = `Timeout when waiting for output "${expected}".\nReceived:\n${this.stdout} \nStderr:\n${this.stderr}`
reject(error)
}, process.env.CI ? 20_000 : 4_000)
const listener = () => {
if (this.stdout.includes(expected)) {
if (timeout)
clearTimeout(timeout)
resolve()
}
}
this.stdoutListeners.push(listener)
})
},
waitForStderr(expected: string) {
const error = new Error('Timeout')
Error.captureStackTrace(error, this.waitForStderr)
return new Promise<void>((resolve, reject) => {
if (this.stderr.includes(expected))
return resolve()
const timeout = setTimeout(() => {
error.message = `Timeout when waiting for error "${expected}".\nReceived:\n${this.stderr}\nStdout:\n${this.stdout}`
reject(error)
}, process.env.CI ? 20_000 : 4_000)
const listener = () => {
if (this.stderr.includes(expected)) {
if (timeout)
clearTimeout(timeout)
resolve()
}
}
this.stderrListeners.push(listener)
})
},
resetOutput() {
this.stdout = ''
this.stderr = ''
},
}
subprocess.stdout!.on('data', (data) => {
cli.stdout += stripAnsi(data.toString())
cli.stdoutListeners.forEach(fn => fn())
})
subprocess.stderr!.on('data', (data) => {
cli.stderr += stripAnsi(data.toString())
cli.stderrListeners.forEach(fn => fn())
})
subprocess.on('exit', () => setDone())
// Manually stop the processes so that each test don't have to do this themselves
afterEach(async () => {
if (subprocess.exitCode === null)
subprocess.kill()
await cli.isDone
})
if (args.includes('--watch')) {
if (command === 'vitest') // Wait for initial test run to complete
await cli.waitForStdout('Waiting for file changes')
// make sure watcher is ready
await cli.waitForStdout('[debug] watcher is ready')
cli.stdout = cli.stdout.replace('[debug] watcher is ready\n', '')
}
else {
await cli.isDone
}
return cli
}
export async function runVitestCli(_options?: Options | string, ...args: string[]) {
process.env.VITE_TEST_WATCHER_DEBUG = 'true'
return runCli('vitest', _options, ...args)
}
export async function runViteNodeCli(_options?: Options | string, ...args: string[]) {
process.env.VITE_TEST_WATCHER_DEBUG = 'true'
return runCli('vite-node', _options, ...args)
}
const originalFiles = new Map<string, string>()
const createdFiles = new Set<string>()
afterEach(() => {
originalFiles.forEach((content, file) => {
fs.writeFileSync(file, content, 'utf-8')
})
createdFiles.forEach((file) => {
fs.unlinkSync(file)
})
originalFiles.clear()
createdFiles.clear()
})
export function createFile(file: string, content: string) {
createdFiles.add(file)
fs.writeFileSync(file, content, 'utf-8')
}
export function editFile(file: string, callback: (content: string) => string) {
const content = fs.readFileSync(file, 'utf-8')
if (!originalFiles.has(file))
originalFiles.set(file, content)
fs.writeFileSync(file, callback(content), 'utf-8')
}