vitest-dev/vitest · error · Error

Unexpected message ${JSON.stringify(message, null, 2)}

Error message

Unexpected message ${JSON.stringify(message, null, 2)}

What it means

Thrown by the typecheck worker's onMessage dispatcher when the incoming WorkerRequest does not match any handled type ('start','run','collect','stop'). The switch in onMessage fell through, meaning an unexpected or malformed message was delivered to the in-process typechecker worker.

Source

Thrown at packages/vitest/src/node/pools/workers/typecheckWorker.ts:100

      return { type: 'testfileFinished', error, __vitest_worker_response__ }
    }

    case 'collect': {
      runPromise = runner.collectTests(message.context.files, project)
        .catch(error => error)
      const error = await runPromise

      return { type: 'testfileFinished', error, __vitest_worker_response__ }
    }

    case 'stop': {
      await runPromise
      return { type: 'stopped', __vitest_worker_response__ }
    }
  }

  throw new Error(`Unexpected message ${JSON.stringify(message, null, 2)}`)
}

function createRunner(vitest: Vitest) {
  const promisesMap = new WeakMap<TestProject, DeferPromise<void>>()
  const rerunTriggered = new WeakSet<TestProject>()

  async function onParseEnd(
    project: TestProject,
    { files, sourceErrors }: TypecheckResults,
  ) {
    const checker = project.typechecker!

    const { packs, events } = checker.getTestPacksAndEvents()
    await vitest._testRun.updated(packs, events)

    if (!project.config.typecheck.ignoreSourceErrors) {
      sourceErrors.forEach(error =>
        vitest.state.catchError(error, 'Unhandled Source Error'),

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Report as a Vitest bug with reproduction, including the JSON of the unexpected message.
  2. Ensure you are not mixing Vitest versions across a workspace (hoisted vs nested installs).
  3. Reinstall dependencies to rule out a stale/partial build of the typecheck runtime.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await vitest.start()
} catch (e) {
  if (e.message.startsWith('Unexpected message')) {
    console.error('Typecheck worker protocol mismatch. Check for mixed Vitest versions.')
    console.error('Message:', e.message)
  }
  throw e
}

Prevention

When it happens

Trigger: TypecheckPoolWorker.send -> onMessage receives a message whose type is not one of 'start' | 'run' | 'collect' | 'stop', or whose __vitest_worker_request__ is true but the type is unknown, reaching packages/vitest/src/node/pools/workers/typecheckWorker.ts:100.

Common situations: An internal protocol mismatch (Vitest runtime version != typecheck worker version); a regression adding a new message type without updating the dispatcher; experimental API misuse sending a custom request type through the typecheck pool.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/eee85fec44bbe9f2.json. Report an issue: GitHub.