-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
setup.ts
132 lines (119 loc) · 3.11 KB
/
setup.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
import { Console } from 'console'
import { Writable } from 'stream'
import { environments } from '../integrations/env'
import type { ResolvedConfig } from '../types'
import { getWorkerState, toArray } from '../utils'
import * as VitestIndex from '../index'
import { resetRunOnceCounter } from '../integrations/run-once'
import { RealDate } from '../integrations/mockdate'
import { rpc } from './rpc'
let globalSetup = false
export async function setupGlobalEnv(config: ResolvedConfig) {
resetRunOnceCounter()
Object.defineProperty(globalThis, '__vitest_index__', {
value: VitestIndex,
enumerable: false,
})
// should be re-declared for each test
// if run with "threads: false"
setupDefines(config.defines)
if (globalSetup)
return
globalSetup = true
setupConsoleLogSpy()
if (config.globals)
(await import('../integrations/globals')).registerApiGlobally()
}
function setupDefines(defines: Record<string, any>) {
for (const key in defines)
(globalThis as any)[key] = defines[key]
}
export function setupConsoleLogSpy() {
const stdoutBuffer: any[] = []
const stderrBuffer: any[] = []
let stdoutTime = 0
let stderrTime = 0
let timer: any
// group sync console.log calls with macro task
function schedule() {
clearTimeout(timer)
timer = setTimeout(() => {
if (stderrTime < stdoutTime) {
sendStderr()
sendStdout()
}
else {
sendStdout()
sendStderr()
}
})
}
function sendStdout() {
if (stdoutBuffer.length) {
rpc().onUserConsoleLog({
type: 'stdout',
content: stdoutBuffer.map(i => String(i)).join(''),
taskId: getWorkerState().current?.id,
time: stdoutTime || RealDate.now(),
})
}
stdoutBuffer.length = 0
stdoutTime = 0
}
function sendStderr() {
if (stderrBuffer.length) {
rpc().onUserConsoleLog({
type: 'stderr',
content: stderrBuffer.map(i => String(i)).join(''),
taskId: getWorkerState().current?.id,
time: stderrTime || RealDate.now(),
})
}
stderrBuffer.length = 0
stderrTime = 0
}
const stdout = new Writable({
write(data, encoding, callback) {
stdoutTime = stdoutTime || RealDate.now()
stdoutBuffer.push(data)
schedule()
callback()
},
})
const stderr = new Writable({
write(data, encoding, callback) {
stderrTime = stderrTime || RealDate.now()
stderrBuffer.push(data)
schedule()
callback()
},
})
globalThis.console = new Console({
stdout,
stderr,
colorMode: true,
groupIndentation: 2,
})
}
export async function withEnv(
name: ResolvedConfig['environment'],
options: ResolvedConfig['environmentOptions'],
fn: () => Promise<void>,
) {
const env = await environments[name].setup(globalThis, options)
try {
await fn()
}
finally {
await env.teardown(globalThis)
}
}
export async function runSetupFiles(config: ResolvedConfig) {
const files = toArray(config.setupFiles)
await Promise.all(
files.map(async(file) => {
getWorkerState().moduleCache.delete(file)
await import(file)
}),
)
}