{"id":"9eb1b119b5c3f069","repo":"vitest-dev/vitest","slug":"vitest-unknown-mock-type-type","errorCode":null,"errorMessage":"[vitest] Unknown mock type: ${type}","messagePattern":"\\[vitest\\] Unknown mock type: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/registry.ts","lineNumber":130,"sourceCode":"      return mock\n    }\n    else if (type === 'automock' || type === 'autospy') {\n      const mock = type === 'automock'\n        ? new AutomockedModule(raw, id, url)\n        : new AutospiedModule(raw, id, url)\n      this.add(mock)\n      return mock\n    }\n    else if (type === 'redirect') {\n      if (typeof factoryOrRedirect !== 'string') {\n        throw new TypeError('[vitest] Redirect mocks require a redirect string.')\n      }\n      const mock = new RedirectedModule(raw, id, url, factoryOrRedirect)\n      this.add(mock)\n      return mock\n    }\n    else {\n      throw new Error(`[vitest] Unknown mock type: ${type}`)\n    }\n  }\n\n  public delete(id: string): void {\n    this.registryByUrl.delete(id)\n  }\n\n  public deleteById(id: string): void {\n    this.registryById.delete(id)\n  }\n\n  public get(id: string): MockedModule | undefined {\n    return this.registryByUrl.get(id)\n  }\n\n  public getById(id: string): MockedModule | undefined {\n    return this.registryById.get(id)\n  }","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/registry.ts#L112-L148","documentation":"In the positional register overload, `type` must be one of `'manual'`, `'automock'`, `'autospy'`, or `'redirect'`. Any other string (typo, casing, future type) reaches the final `else` branch and is rejected.","triggerScenarios":"Calling `registry.register('Manual', raw, id, url, factory)` (wrong casing) or `registry.register('stub', ...)`, or passing `undefined` as the first arg when the others are positional.","commonSituations":"Typo in programmatic mock setup; dynamically computed type strings; accidental `undefined` first argument.","solutions":["Use exactly one of: `'manual'`, `'automock'`, `'autospy'`, `'redirect'`.","If `type` comes from a variable, constrain it to the `MockedModuleType` union.","Prefer the serialized-object overload only when you have a real DTO; otherwise use positional with a literal type."],"exampleFix":"// before\nregistry.register('automocking', raw, id, url)\n\n// after\nregistry.register('automock', raw, id, url)","handlingStrategy":"validation","validationCode":"const VALID_TYPES = new Set(['manual', 'automock', 'autospy', 'redirect'])\n\nfunction safeRegisterPositional(registry: any, type: unknown, ...rest: unknown[]) {\n  if (typeof type !== 'string' || !VALID_TYPES.has(type)) {\n    throw new Error(`Unknown mock type '${type}'. Valid: ${[...VALID_TYPES].join(', ')}`)\n  }\n  ;(registry.register as any)(type, ...rest)\n}","typeGuard":"function isMockedModuleType(v: unknown): v is 'manual' | 'automock' | 'autospy' | 'redirect' {\n  return typeof v === 'string'\n    && ['manual', 'automock', 'autospy', 'redirect'].includes(v)\n}","tryCatchPattern":null,"preventionTips":["Constrain the `type` parameter to the `MockedModuleType` union in all wrappers.","Compute type from a constant rather than a free-form string.","Add exhaustiveness checks (`switch`) so new types surface at compile time."],"tags":["mocking","registry","validation"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}