vitest-dev/vitest · error · Error

[vitest] Unknown mock type

Error message

[vitest] Unknown mock type: ${type}

What it means

Internal invariant: MockRegistry.register received a type that is not one of 'manual', 'automock', 'autospy', or 'redirect'. This is a defensive guard for the else-branch after all known types are handled and should not occur from public API usage.

Example fix

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

Strategy: validation

Validate before calling

const KNOWN_TYPES = new Set(['manual', 'automock', 'autospy', 'redirect'])
if (!KNOWN_TYPES.has(type)) {
  throw new Error(`Unsupported mock type '${type}'. Expected one of: ${[...KNOWN_TYPES].join(', ')}`)
}
registry.register({ type, raw, id, url }, factoryOrRedirect)

Type guard

function isMockType(v: unknown): v is 'manual'|'automock'|'autospy'|'redirect' { return typeof v === 'string' && new Set(['manual','automock','autospy','redirect']).has(v) }

Prevention

When it happens

Trigger: Directly invoking registry.register with a type string outside the allowed union; corrupted serialized mock events; misuse of the internal register API; a typo in the type field of a mock descriptor.

Common situations: Internal code or a plugin passing an invalid type; a serialized mock event with an unrecognized type field; version mismatch where a newer mock type is sent to an older registry.

Related errors


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

Appendix: source

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

      return mock
    }
    else if (type === 'automock' || type === 'autospy') {
      const mock = type === 'automock'
        ? new AutomockedModule(raw, id, url)
        : new AutospiedModule(raw, id, url)
      this.add(mock)
      return mock
    }
    else if (type === 'redirect') {
      if (typeof factoryOrRedirect !== 'string') {
        throw new TypeError('[vitest] Redirect mocks require a redirect string.')
      }
      const mock = new RedirectedModule(raw, id, url, factoryOrRedirect)
      this.add(mock)
      return mock
    }
    else {
      throw new Error(`[vitest] Unknown mock type: ${type}`)
    }
  }

  public delete(id: string): void {
    this.registryByUrl.delete(id)
  }

  public deleteById(id: string): void {
    this.registryById.delete(id)
  }

  public get(id: string): MockedModule | undefined {
    return this.registryByUrl.get(id)
  }

  public getById(id: string): MockedModule | undefined {
    return this.registryById.get(id)
  }

View on GitHub (pinned to 1fa9837ec2)