-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
entry.ts
52 lines (42 loc) · 1.59 KB
/
entry.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
import { promises as fs } from 'fs'
import type { BuiltinEnvironment, ResolvedConfig } from '../types'
import { getWorkerState, resetModules } from '../utils'
import { envs } from '../integrations/env'
import { setupGlobalEnv, withEnv } from './setup'
import { startTests } from './run'
export async function run(files: string[], config: ResolvedConfig): Promise<void> {
await setupGlobalEnv(config)
const workerState = getWorkerState()
// if calling from a worker, there will always be one file
// if calling with no-threads, this will be the whole suite
const filesWithEnv = await Promise.all(files.map(async (file) => {
const code = await fs.readFile(file, 'utf-8')
const env = code.match(/@(?:vitest|jest)-environment\s+?([\w-]+)\b/)?.[1] || config.environment || 'node'
if (!envs.includes(env))
throw new Error(`Unsupported environment: "${env}" in ${file}`)
return {
file,
env: env as BuiltinEnvironment,
}
}))
const filesByEnv = filesWithEnv.reduce((acc, { file, env }) => {
acc[env] ||= []
acc[env].push(file)
return acc
}, {} as Record<BuiltinEnvironment, string[]>)
for (const env of envs) {
const environment = env as BuiltinEnvironment
const files = filesByEnv[environment]
if (!files || !files.length)
continue
await withEnv(environment, config.environmentOptions || {}, async () => {
for (const file of files) {
workerState.mockMap.clear()
resetModules()
workerState.filepath = file
await startTests([file], config)
workerState.filepath = undefined
}
})
}
}