{"record":{"id":"2e897407853a0c19","repo":"denoland/deno","slug":"cannot-mock-property-string-methodname-becau","errorCode":null,"errorMessage":"Cannot mock property '${String(methodName)}' because it does not exist","messagePattern":"Cannot mock property '(.+?)' because it does not exist","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/testing.ts","lineNumber":2498,"sourceCode":"    const desc = ObjectGetOwnPropertyDescriptor(current, name);\n    if (desc) return desc;\n    current = ObjectGetPrototypeOf(current);\n  }\n  return undefined;\n}\n\nfunction mockMethodImpl(object, methodName, implementation, options) {\n  if (\n    implementation !== null && typeof implementation === \"object\" &&\n    typeof implementation !== \"function\"\n  ) {\n    options = implementation;\n    implementation = undefined;\n  }\n\n  const descriptor = findPropertyDescriptor(object, methodName);\n  if (!descriptor) {\n    throw new TypeError(\n      `Cannot mock property '${String(methodName)}' because it does not exist`,\n    );\n  }\n\n  const isGetter = options?.getter ?? false;\n  const isSetter = options?.setter ?? false;\n\n  let original;\n  if (isGetter) {\n    original = descriptor.get;\n  } else if (isSetter) {\n    original = descriptor.set;\n  } else {\n    original = descriptor.value;\n  }\n\n  if (typeof original !== \"function\") {\n    throw new TypeError(","sourceCodeStart":2480,"sourceCodeEnd":2516,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/testing.ts#L2480-L2516","documentation":"mock.method() resolves the method via findPropertyDescriptor(), which walks the object's prototype chain, and throws this plain TypeError when no property with that name exists anywhere. You can only replace a method that is actually present, matching Node's behavior.","triggerScenarios":"mock.method(math, 'addition') (typo for 'add'); mock.method(db, 'save') where save exists only as a Proxy get-trap so no real descriptor is found; mocking the instance when the method lives on an object the code never receives.","commonSituations":"Upstream renames/refactors of the mocked API; wrong receiver object passed to mock.method; case-sensitive name mistakes; dynamically created runtime methods.","solutions":["Verify the name exists before mocking: if (!('add' in math)) fail with a clear message","Fix the typo / use the exact, case-sensitive method name","Mock the object that actually owns the method (walk Object.getPrototypeOf if needed)","For members that only appear via a Proxy trap, restructure so a real property exists - descriptor lookup cannot see traps"],"exampleFix":"// before\nmock.method(math, 'addition', () => 3); // no such method\n\n// after\nmock.method(math, 'add', () => 3);","handlingStrategy":"validation","validationCode":"function assertMockableMethod(o: object, name: PropertyKey): void {\n  let t: object | null = o;\n  while (t !== null) {\n    if (name in t) return;\n    t = Object.getPrototypeOf(t);\n  }\n  throw new Error(`refusing to mock missing method ${String(name)}`);\n}","typeGuard":"function hasMethod(o: object, name: string): boolean {\n  let t: object | null = o;\n  while (t !== null) {\n    if (name in t && typeof (t as Record<string, unknown>)[name] === 'function') return true;\n    t = Object.getPrototypeOf(t);\n  }\n  return false;\n}","tryCatchPattern":"try { mock.method(obj, name, impl); } catch (e) { if (e instanceof TypeError && /does not exist/.test(e.message)) throw new Error(`API surface changed: ${name}`); else throw e; }","preventionTips":["Fail fast on missing names with a clear message instead of letting the mock throw","Keep a typed interface of mocked surfaces and derive keys from it","Re-run the mock suite after upgrading dependencies that rename APIs"],"tags":["node-test","mock","mock-method","missing-property"],"backgroundTag":"property-not-found","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}