/
worker.ts
111 lines (94 loc) · 3.21 KB
/
worker.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
import { relative, resolve } from 'pathe'
import { createBirpc } from 'birpc'
import { workerId as poolId } from 'tinypool'
import { processError } from '@vitest/runner/utils'
import { ModuleCacheMap } from 'vite-node/client'
import { isPrimitive } from 'vite-node/utils'
import type { ResolvedConfig, WorkerContext, WorkerRPC } from '../types'
import { distDir } from '../constants'
import { getWorkerState } from '../utils/global'
import type { MockMap } from '../types/mocker'
import { executeInViteNode } from './execute'
import { rpc } from './rpc'
let _viteNode: {
run: (files: string[], config: ResolvedConfig) => Promise<void>
}
const moduleCache = new ModuleCacheMap()
const mockMap: MockMap = new Map()
async function startViteNode(ctx: WorkerContext) {
if (_viteNode)
return _viteNode
const { config } = ctx
const processExit = process.exit
process.exit = (code = process.exitCode || 0): never => {
const error = new Error(`process.exit called with "${code}"`)
rpc().onWorkerExit(error, code)
return processExit(code)
}
function catchError(err: unknown, type: string) {
const worker = getWorkerState()
const error = processError(err)
if (worker.filepath && !isPrimitive(error)) {
error.VITEST_TEST_NAME = worker.current?.name
error.VITEST_TEST_PATH = relative(config.root, worker.filepath)
}
rpc().onUnhandledError(error, type)
}
process.on('uncaughtException', e => catchError(e, 'Uncaught Exception'))
process.on('unhandledRejection', e => catchError(e, 'Unhandled Rejection'))
const { run } = (await executeInViteNode({
files: [
resolve(distDir, 'entry.js'),
],
fetchModule(id) {
return rpc().fetch(id)
},
resolveId(id, importer) {
return rpc().resolveId(id, importer)
},
moduleCache,
mockMap,
interopDefault: config.deps.interopDefault,
root: config.root,
base: config.base,
}))[0]
_viteNode = { run }
return _viteNode
}
function init(ctx: WorkerContext) {
// @ts-expect-error untyped global
if (typeof __vitest_worker__ !== 'undefined' && ctx.config.threads && ctx.config.isolate)
throw new Error(`worker for ${ctx.files.join(',')} already initialized by ${getWorkerState().ctx.files.join(',')}. This is probably an internal bug of Vitest.`)
const { config, port, workerId } = ctx
process.env.VITEST_WORKER_ID = String(workerId)
process.env.VITEST_POOL_ID = String(poolId)
// @ts-expect-error untyped global
globalThis.__vitest_environment__ = config.environment
// @ts-expect-error I know what I am doing :P
globalThis.__vitest_worker__ = {
ctx,
moduleCache,
config,
mockMap,
rpc: createBirpc<WorkerRPC>(
{},
{
eventNames: ['onUserConsoleLog', 'onFinished', 'onCollected', 'onWorkerExit'],
post(v) { port.postMessage(v) },
on(fn) { port.addListener('message', fn) },
},
),
}
if (ctx.invalidates) {
ctx.invalidates.forEach((fsPath) => {
moduleCache.delete(fsPath)
moduleCache.delete(`mock:${fsPath}`)
})
}
ctx.files.forEach(i => moduleCache.delete(i))
}
export async function run(ctx: WorkerContext) {
init(ctx)
const { run } = await startViteNode(ctx)
return run(ctx.files, ctx.config)
}