{"record":{"id":"989009ec131275ae","repo":"vitest-dev/vitest","slug":"unknown-mock-type-mock-as-any-type","errorCode":null,"errorMessage":"Unknown mock type: ${(mock as any).type}","messagePattern":"Unknown mock type: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/browser/interceptor-msw.ts","lineNumber":109,"sourceCode":"      const worker = setupWorker(\n        http.get(/.+/, async ({ request }) => {\n          const path = cleanQuery(request.url.slice(location.origin.length))\n          if (!this.mocks.has(path)) {\n            return passthrough()\n          }\n\n          const mock = this.mocks.get(path)!\n\n          switch (mock.type) {\n            case 'manual':\n              return this.resolveManualMock(mock)\n            case 'automock':\n            case 'autospy':\n              return Response.redirect(injectQuery(path, `mock=${mock.type}`))\n            case 'redirect':\n              return Response.redirect(mock.redirect)\n            default:\n              throw new Error(`Unknown mock type: ${(mock as any).type}`)\n          }\n        }),\n      )\n      return worker.start(this.options.mswOptions).then(() => worker)\n    }).finally(() => {\n      this.worker = worker\n      this.startPromise = undefined\n    })\n    return await this.startPromise\n  }\n}\n\nconst trailingSeparatorRE = /[?&]$/\nconst timestampRE = /\\bt=\\d{13}&?\\b/\nconst versionRE = /\\bv=\\w{8}&?\\b/\nfunction cleanQuery(url: string) {\n  return url.replace(timestampRE, '').replace(versionRE, '').replace(trailingSeparatorRE, '')\n}","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/browser/interceptor-msw.ts#L91-L127","documentation":"Thrown by the MSW-based browser module interceptor when a registered mock's `type` does not match any of the handled cases (`manual`, `automock`, `autospy`, `redirect`) in the request-handler switch. The interceptor serves mocked modules over HTTP in browser mode; reaching the `default` branch means the registry holds a mock whose type is outside the `MockedModuleType` union. This is a defensive guard that should be unreachable under normal use.","triggerScenarios":"A mock object with an unexpected `type` string is stored in the `ModuleMockerMSWInterceptor.mocks` registry and then served by the `http.get(/.+/)` handler. This can happen if a custom/forked interceptor registers a non-standard mock class, or if the registry is corrupted by a race during concurrent `register()`/`invalidate()` calls.","commonSituations":"Running Vitest browser mode with a forked or patched mocker that introduces a new mock type without extending the switch; concurrent test file teardown that mutates the registry while a request is in flight; a Vitest internal regression after adding a new `MockedModuleType` without updating the interceptor.","solutions":["If you forked Vitest or wrote a custom interceptor, extend the switch in `interceptor-msw.ts` to handle your custom mock type.","Update Vitest to the latest patch release — this branch indicates an internal inconsistency that may already be fixed.","File a Vitest bug report with a minimal browser-mode reproduction, since the four union types should make this unreachable in stock Vitest."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"// Before adding a mock to the MSW interceptor, confirm its type is one the handler switch covers.\nconst HANDLED_MOCK_TYPES = new Set(['manual', 'automock', 'autospy', 'redirect'])\nfunction isHandledMockType(mock: { type: string }): boolean {\n  return HANDLED_MOCK_TYPES.has(mock.type)\n}\nif (!isHandledMockType(mock)) {\n  throw new Error(`Refusing to register mock with unsupported type: ${mock.type}`)\n}","typeGuard":"function isKnownMockType(mock: unknown): mock is { type: 'manual' | 'automock' | 'autospy' | 'redirect' } {\n  return typeof mock === 'object' && mock !== null\n    && typeof (mock as any).type === 'string'\n    && ['manual', 'automock', 'autospy', 'redirect'].includes((mock as any).type)\n}","tryCatchPattern":null,"preventionTips":["Treat the four-type MockedModuleType union as exhaustive; any new type must update both the registry and every interceptor switch.","In forked code, add a type-narrowing wrapper around registry.add so unsupported types fail at registration, not at request time.","Keep browser-side and node-side mocker versions in sync to avoid type-set drift."],"tags":["browser-mode","msw","mock-registry","internal-guard"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}