{"record":{"id":"2fb8d80331c7262e","repo":"vitest-dev/vitest","slug":"vi-when-type-is-not-a-known-method","errorCode":null,"errorMessage":"vi.when: \"${type}\" is not a known method","messagePattern":"vi\\.when: \"(.+?)\" is not a known method","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/integrations/mock/when.ts","lineNumber":515,"sourceCode":"\nfunction getMethodName(type: BehaviorType): string {\n  switch (type) {\n    case 'return': {\n      return 'thenReturn'\n    }\n    case 'resolve': {\n      return 'thenResolve'\n    }\n    case 'throw': {\n      return 'thenThrow'\n    }\n    case 'reject': {\n      return 'thenReject'\n    }\n    default: {\n      (type satisfies never)\n\n      throw new Error(`vi.when: \"${type}\" is not a known method`)\n    }\n  }\n}\n\nfunction hasBeenConsumed(action: BehaviorAction<unknown>): boolean {\n  return action.remaining === 0 /* times-actions reached 0 */\n    || (action.remaining === Number.POSITIVE_INFINITY && action.called) /* infinite actions called at least once */\n}\n\nfunction getRemainingLabel(action: BehaviorAction<unknown>): string {\n  if (hasBeenConsumed(action)) {\n    return action.remaining === Number.POSITIVE_INFINITY\n      ? 'exhausted'\n      : `exhausted (${action.times} of ${action.times})`\n  }\n\n  return action.remaining === Number.POSITIVE_INFINITY\n    ? 'never called'","sourceCodeStart":497,"sourceCodeEnd":533,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/integrations/mock/when.ts#L497-L533","documentation":"A defensive exhaustiveness guard inside `getMethodName`, which maps each `BehaviorType` (`'return' | 'throw' | 'resolve' | 'reject'`) to its public `then*` method name. The `default` branch is paired with `(type satisfies never)`: under TypeScript this should be unreachable, and at runtime it only fires if an invalid `BehaviorType` somehow reaches the function — indicating an internal Vitest bug or memory corruption of a behavior object.","triggerScenarios":"Effectively unreachable in normal usage. Could only occur if a behavior object's `type` field was mutated to a non-union string, a third-party plugin injected a behavior with an invalid type, or an internal code path constructed a `BehaviorAction` without going through the public `then*` methods.","commonSituations":"Should not occur for end users. If seen, suspect a Vitest internal regression, a patched/monkey-patched Vitest, or corrupted module state from HMR.","solutions":["Report a bug to Vitest with a minimal reproduction — this is not an expected user-facing error.","Ensure you are not mutating behavior objects or passing raw objects into Vitest internals.","Update to the latest patch of Vitest in case it is a fixed internal regression."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  // normal vi.when usage\n} catch (e) {\n  if (e instanceof Error && /is not a known method/.test(e.message)) {\n    // internal Vitest bug: report upstream with a reproduction\n  }\n  throw e\n}","preventionTips":["Do not mutate behavior objects or pass raw objects into Vitest internals.","Keep Vitest updated to the latest patch.","Avoid monkey-patching Vitest internals or third-party plugins that inject behaviors."],"tags":["vi-when","internal","exhaustiveness","unreachable","bug"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}