vitest-dev/vitest · error · TypeError

Test worker should expose "runTests" method. Received

Error message

Test worker should expose "runTests" method. Received "${typeof worker.runTests}".

What it means

At worker startup Vitest selects 'runTests' or 'collectTests' on the resolved worker object based on the run method, then asserts the method exists and is a function before calling it. If the worker module does not expose that method this TypeError is thrown, reporting the actual typeof of worker.runTests.

Solutions

  1. Rebuild Vitest packages (pnpm build / pnpm dev) so the worker entry is current.
  2. Ensure all @vitest/* and vitest packages share the same version (dedupe dependencies).
  3. Clear node_modules/.vite and the vitest cache, then reinstall.
  4. If using a custom worker, make sure it re-exports runTests and collectTests from the Vitest runtime.

Example fix

// before: stale worker bundle missing runTests
// (no code change; rebuild)

// after: rebuild so the export exists
// $ pnpm build
Defensive patterns

Strategy: type-guard

Type guard

function isWorkerCallable(worker, method) {
  const name = method === 'collect' ? 'collectTests' : 'runTests'
  return typeof worker?.[name] === 'function'
}

Prevention

When it happens

Trigger: The worker entry module (the Vitest runtime) is loaded but does not export a function named runTests (for run) or collectTests (for collect). This happens when the worker code is a stale/corrupt build, a custom worker shim is used, or the wrong entry file is loaded due to a version mismatch between vitest core and the runner package.

Common situations: Partial/failed `pnpm build` in the monorepo leaving an outdated worker bundle, mixing Vitest package versions (e.g. @vitest/runner newer than vitest), or a third-party integration that replaces the worker entry.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/660b117215ef0e8f. Report an issue: GitHub.

Appendix: source

Thrown at packages/vitest/src/runtime/worker.ts:60

        fetch: 0,
      },
      rpc,
      onCancel,
      onCleanup: listeners.onCleanup,
      providedContext: ctx.providedContext,
      onFilterStackTrace(stack) {
        return createStackString(parseStacktrace(stack))
      },
      metaEnv: ctx.metaEnv,
      getterTracker: ctx.config.benchmark.enabled && !ctx.config.benchmark.suppressExportGetterWarnings
        ? new GetterTracker()
        : undefined,
    } satisfies WorkerGlobalState

    const methodName = method === 'collect' ? 'collectTests' : 'runTests'

    if (!worker[methodName] || typeof worker[methodName] !== 'function') {
      throw new TypeError(
        `Test worker should expose "runTests" method. Received "${typeof worker.runTests}".`,
      )
    }

    await worker[methodName](state, traces)
  }
  finally {
    await rpcDone().catch(() => {})
    await Promise.all(cleanups.map(fn => fn())).catch(() => {})
  }
}

export function run(ctx: ContextRPC, worker: VitestWorker, traces: Traces): Promise<void> {
  return execute('run', ctx, worker, traces)
}

export function collect(ctx: ContextRPC, worker: VitestWorker, traces: Traces): Promise<void> {
  return execute('collect', ctx, worker, traces)

View on GitHub (pinned to 1fa9837ec2)