(method: 'run' | 'collect', ctx: ContextRPC, worker: VitestWorker, traces: Traces)
| 11 | const resolvingModules = new Set<string>() |
| 12 | |
| 13 | async 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 | |
| 68 | export function run(ctx: ContextRPC, worker: VitestWorker, traces: Traces): Promise<void> { |
| 69 | return execute('run', ctx, worker, traces) |
no test coverage detected