MCPcopy
hub / github.com/vitest-dev/vitest / execute

Function execute

packages/vitest/src/runtime/worker.ts:13–66  ·  view source on GitHub ↗
(method: 'run' | 'collect', ctx: ContextRPC, worker: VitestWorker, traces: Traces)

Source from the content-addressed store, hash-verified

11const resolvingModules = new Set<string>()
12
13async function execute(method: 'run' | 'collect', ctx: ContextRPC, worker: VitestWorker, traces: Traces) {
14 const prepareStart = performance.now()
15
16 const cleanups: (() => void | Promise<void>)[] = [setupInspect(ctx)]
17
18 // RPC is used to communicate between worker (be it a thread worker or child process or a custom implementation) and the main thread
19 const rpc = ctx.rpc
20
21 try {
22 // do not close the RPC channel so that we can get the error messages sent to the main thread
23 cleanups.push(async () => {
24 await Promise.all(rpc.$rejectPendingCalls(({ method, reject }) => {
25 reject(new EnvironmentTeardownError(`[vitest-worker]: Closing rpc while "${method}" was pending`))
26 }))
27 })
28
29 const state = {
30 ctx,
31 // here we create a new one, workers can reassign this if they need to keep it non-isolated
32 evaluatedModules: new VitestEvaluatedModules(),
33 resolvingModules,
34 moduleExecutionInfo: new Map(),
35 config: ctx.config,
36 // this is set later by vm or base
37 environment: null!,
38 durations: {
39 environment: 0,
40 prepare: prepareStart,
41 },
42 rpc,
43 onCancel,
44 onCleanup: listeners.onCleanup,
45 providedContext: ctx.providedContext,
46 onFilterStackTrace(stack) {
47 return createStackString(parseStacktrace(stack))
48 },
49 metaEnv: createImportMetaEnvProxy(),
50 } satisfies WorkerGlobalState
51
52 const methodName = method === 'collect' ? 'collectTests' : 'runTests'
53
54 if (!worker[methodName] || typeof worker[methodName] !== 'function') {
55 throw new TypeError(
56 `Test worker should expose "runTests" method. Received "${typeof worker.runTests}".`,
57 )
58 }
59
60 await worker[methodName](state, traces)
61 }
62 finally {
63 await rpcDone().catch(() => {})
64 await Promise.all(cleanups.map(fn => fn())).catch(() => {})
65 }
66}
67
68export function run(ctx: ContextRPC, worker: VitestWorker, traces: Traces): Promise<void> {
69 return execute('run', ctx, worker, traces)

Callers 2

runFunction · 0.85
collectFunction · 0.85

Calls 6

setupInspectFunction · 0.90
rpcDoneFunction · 0.90
allMethod · 0.80
createImportMetaEnvProxyFunction · 0.70
rejectFunction · 0.50
fnFunction · 0.50

Tested by

no test coverage detected