{"id":"660b117215ef0e8f","repo":"vitest-dev/vitest","slug":"test-worker-should-expose-runtests-method-recei","errorCode":null,"errorMessage":"Test worker should expose \"runTests\" method. Received \"${typeof worker.runTests}\".","messagePattern":"Test worker should expose \"runTests\" method\\. Received \"(.+?)\"\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/runtime/worker.ts","lineNumber":59,"sourceCode":"        prepare: prepareStart,\n      },\n      rpc,\n      onCancel,\n      onCleanup: listeners.onCleanup,\n      providedContext: ctx.providedContext,\n      onFilterStackTrace(stack) {\n        return createStackString(parseStacktrace(stack))\n      },\n      metaEnv: ctx.metaEnv,\n      getterTracker: ctx.config.benchmark.enabled && !ctx.config.benchmark.suppressExportGetterWarnings\n        ? new GetterTracker()\n        : undefined,\n    } satisfies WorkerGlobalState\n\n    const methodName = method === 'collect' ? 'collectTests' : 'runTests'\n\n    if (!worker[methodName] || typeof worker[methodName] !== 'function') {\n      throw new TypeError(\n        `Test worker should expose \"runTests\" method. Received \"${typeof worker.runTests}\".`,\n      )\n    }\n\n    await worker[methodName](state, traces)\n  }\n  finally {\n    await rpcDone().catch(() => {})\n    await Promise.all(cleanups.map(fn => fn())).catch(() => {})\n  }\n}\n\nexport function run(ctx: ContextRPC, worker: VitestWorker, traces: Traces): Promise<void> {\n  return execute('run', ctx, worker, traces)\n}\n\nexport function collect(ctx: ContextRPC, worker: VitestWorker, traces: Traces): Promise<void> {\n  return execute('collect', ctx, worker, traces)","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/src/runtime/worker.ts#L41-L77","documentation":"Thrown by the runtime execute() dispatcher when the provided worker object does not expose a callable runTests (for 'run') or collectTests (for 'collect') method. The VitestWorker contract (packages/vitest/src/runtime/workers/types.ts) requires both runTests and collectTests functions, and this guard rejects workers that only partially implement it.","triggerScenarios":"Implementing a custom pool/worker via the experimental VitestWorker interface and passing a worker object that is missing the runTests method, has it set to undefined, or assigns a non-function value. Reached at runtime/worker.ts:58 inside execute() before any test runs.","commonSituations":"Writing a custom worker pool and forgetting to wire runTests/collectTests, or a build/bundling step that tree-shook the methods off the exported worker object, or passing the wrong object to the runtime run/collect entry points.","solutions":["Ensure your custom worker object defines both runTests and collectTests as functions per the VitestWorker interface.","Confirm you are passing the actual worker instance (not a prototype/options object) to runtime run()/collect().","Re-export the standard worker init (init-threads/init-forks) which wires these methods automatically instead of hand-rolling them."],"exampleFix":"// before: custom worker missing runTests\nconst worker = { post, on, off, serialize, deserialize }\nruntime.run(ctx, worker, traces)\n\n// after: provide the required methods\nconst worker = {\n  post, on, off, serialize, deserialize,\n  runTests: (state, traces) => runBaseTests('run', state, traces),\n  collectTests: (state, traces) => runBaseTests('collect', state, traces),\n}\nruntime.run(ctx, worker, traces)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"import type { VitestWorker } from 'vitest/workers'\n\nfunction isValidVitestWorker(w: unknown): w is VitestWorker {\n  return !!w\n    && typeof w === 'object'\n    && typeof (w as any).runTests === 'function'\n    && typeof (w as any).collectTests === 'function'\n    && typeof (w as any).post === 'function'\n    && typeof (w as any).on === 'function'\n    && typeof (w as any).off === 'function'\n}\n\nif (!isValidVitestWorker(worker)) {\n  throw new TypeError('worker is not a valid VitestWorker')\n}","tryCatchPattern":null,"preventionTips":["Always satisfy the VitestWorker interface (types.ts) when building a custom pool.","Reuse the built-in init-threads/init-forks wiring rather than constructing the worker object by hand.","Add a unit test that asserts runTests/collectTests are functions before dispatching run()."],"tags":["custom-pool","worker","api-contract","experimental"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}