vitest-dev/vitest · error · Error

Unknown mock type

Error message

Unknown mock type: ${(event as any).type}

What it means

Thrown by `MockerRegistry.register` (object-form) when `event.type` is none of `'automock'`, `'autospy'`, `'redirect'`, `'manual'`. This is a defensive guard indicating the event object's `type` field is outside the known `MockedModuleType` union. Under correct usage the type is exhaustively checked; reaching the `else` means a corrupted, version-mismatched, or hand-crafted event was passed.

Solutions

  1. Ensure the node-side and browser-side Vitest builds are the same version.
  2. If extending the type system, add the new type branch to `register` and to the `MockedModuleType` union.
  3. Validate the event `type` against the known union before calling `register`.
Defensive patterns

Strategy: type-guard

Validate before calling

// Validate event.type against the known union before registering.
const KNOWN_TYPES = new Set(['automock', 'autospy', 'manual', 'redirect'])
if (!KNOWN_TYPES.has(event.type)) {
  throw new Error(`Unknown mock type in event: ${event.type}`)
}
registry.register(event)

Type guard

function isKnownMockedModuleSerialized(event: unknown): event is { type: 'automock' | 'autospy' | 'manual' | 'redirect' } {
  return typeof event === 'object' && event !== null
    && typeof (event as any).type === 'string'
    && ['automock', 'autospy', 'manual', 'redirect'].includes((event as any).type)
}

Prevention

When it happens

Trigger: Passing an object with an unrecognized `type` string to `register`; an RPC/IPC payload whose `type` field does not match any version of the `MockedModuleSerialized` union (e.g., a future type from a newer Vitest running against an older registry).

Common situations: Version skew between the Vitest node-side runner and the browser-side mocker; a custom transport injecting non-standard events; a corrupted or hand-built registry payload.

Related errors


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

Appendix: source

Thrown at packages/mocker/src/registry.ts:90

        const module = AutomockedModule.fromJSON(event)
        this.add(module)
        return module
      }
      else if (event.type === 'autospy') {
        const module = AutospiedModule.fromJSON(event)
        this.add(module)
        return module
      }
      else if (event.type === 'redirect') {
        const module = RedirectedModule.fromJSON(event)
        this.add(module)
        return module
      }
      else if (event.type === 'manual') {
        throw new Error(`Cannot set serialized manual mock. Define a factory function manually with \`ManualMockedModule.fromJSON()\`.`)
      }
      else {
        throw new Error(`Unknown mock type: ${(event as any).type}`)
      }
    }

    if (typeof raw !== 'string') {
      throw new TypeError('[vitest] Mocks require a raw string.')
    }

    if (typeof url !== 'string') {
      throw new TypeError('[vitest] Mocks require a url string.')
    }

    if (typeof id !== 'string') {
      throw new TypeError('[vitest] Mocks require an id string.')
    }

    if (type === 'manual') {
      if (typeof factoryOrRedirect !== 'function') {
        throw new TypeError('[vitest] Manual mocks require a factory function.')

View on GitHub (pinned to 1fa9837ec2)