forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.ts
66 lines (54 loc) · 2.05 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { promises as fs } from 'fs'
import type { ResolvedConfig, VitestEnvironment } 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()
// TODO @web-runner: we need to figure out how to do this on the browser
if (config.browser) {
workerState.mockMap.clear()
await startTests(files, config)
return
}
// 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'
return {
file,
env: env as VitestEnvironment,
}
}))
const filesByEnv = filesWithEnv.reduce((acc, { file, env }) => {
acc[env] ||= []
acc[env].push(file)
return acc
}, {} as Record<VitestEnvironment, string[]>)
const orderedEnvs = envs.concat(
Object.keys(filesByEnv).filter(env => !envs.includes(env)),
)
for (const env of orderedEnvs) {
const environment = env as VitestEnvironment
const files = filesByEnv[environment]
if (!files || !files.length)
continue
await withEnv(environment, config.environmentOptions || {}, async () => {
for (const file of files) {
// it doesn't matter if running with --threads
// if running with --no-threads, we usually want to reset everything before running a test
// but we have --isolate option to disable this
if (config.isolate) {
workerState.mockMap.clear()
resetModules(workerState.moduleCache, true)
}
workerState.filepath = file
await startTests([file], config)
workerState.filepath = undefined
}
})
}
}