vitest-dev/vitest · error · Error

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

Error message

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

What it means

When rehydrating a serialized mock, the registry dispatches on the object's `type` field and only recognizes `automock`, `autospy`, `redirect`, and `manual`. Any other value — typo, `undefined`, or a future/extension type — falls through to this error.

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 d568f8ce37)

Solutions

  1. Set `type` to one of the supported values: `'automock'`, `'autospy'`, `'redirect'`, or `'manual'`.
  2. If you extended the mock types, update `register()` to handle the new branch instead of relying on the serialized path.
  3. Validate the payload shape before calling register (see defense).

Example fix

// before
registry.register({ type: 'Manual', raw, id, url })

// after
registry.register({ type: 'manual', raw, id, url })
Defensive patterns

Strategy: validation

Validate before calling

const VALID_TYPES = new Set(['automock', 'autospy', 'redirect', 'manual'])

function safeRegister(registry: any, dto: { type: string }) {
  if (!VALID_TYPES.has(dto?.type)) {
    throw new Error(`Unsupported mock type '${dto?.type}'. Valid: ${[...VALID_TYPES].join(', ')}`)
  }
  registry.register(dto)
}

Type guard

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

Prevention

When it happens

Trigger: Calling `registry.register({ type: 'manualMock', ... })` (wrong casing), `{ type: undefined }`, or a hand-built payload with an invented type string.

Common situations: Hand-crafting mock JSON; schema drift between Vitest versions; corrupted IPC payload; third-party tools emitting non-standard mock objects.

Related errors


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