{"record":{"id":"d0cecf21876c7907","repo":"jestjs/jest","slug":"cannot-use-replaceproperty-on-a-primitive-value","errorCode":null,"errorMessage":"Cannot use replaceProperty on a primitive value; ${this._typeOf(object)} given","messagePattern":"Cannot use replaceProperty on a primitive value; (.+?) given","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-mock/src/index.ts","lineNumber":1459,"sourceCode":"        // @ts-expect-error - wrong context\n        return original.apply(this, arguments);\n      });\n    }\n\n    Object.defineProperty(object, propertyKey, descriptor);\n    return descriptor[accessType] as Mock;\n  }\n\n  replaceProperty<T extends object, K extends keyof T>(\n    object: T,\n    propertyKey: K,\n    value: T[K],\n  ): Replaced<T[K]> {\n    if (\n      object == null ||\n      (typeof object !== 'object' && typeof object !== 'function')\n    ) {\n      throw new Error(\n        `Cannot use replaceProperty on a primitive value; ${this._typeOf(\n          object,\n        )} given`,\n      );\n    }\n\n    if (propertyKey == null) {\n      throw new Error('No property name supplied');\n    }\n\n    let descriptor = Object.getOwnPropertyDescriptor(object, propertyKey);\n    let proto = Object.getPrototypeOf(object);\n    while (!descriptor && proto !== null) {\n      descriptor = Object.getOwnPropertyDescriptor(proto, propertyKey);\n      proto = Object.getPrototypeOf(proto);\n    }\n    if (!descriptor) {\n      throw new Error(","sourceCodeStart":1441,"sourceCodeEnd":1477,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/jest-mock/src/index.ts#L1441-L1477","documentation":"Thrown by jest.replaceProperty when the first argument is null, undefined, or a primitive. Like spyOn, replaceProperty needs an object/function to attach the replacement descriptor to.","triggerScenarios":"jest.replaceProperty(null,'x',v); jest.replaceProperty(undefined,'x',v); jest.replaceProperty('str','length',99); jest.replaceProperty(3,'toString',fn).","commonSituations":"The target import resolved to undefined (wrong path, named/default mismatch, ESM/CJS interop); tree-shaking removed the binding; passing a primitive export instead of its holder object.","solutions":["Log the first argument and confirm it is an object/function at runtime.","Fix the import path / named-vs-default usage.","Pass the object that owns the property rather than a primitive."],"exampleFix":"// before\njest.replaceProperty(settings, 'timeout', 5000); // settings is undefined\n// after\nimport * as settings from './settings';\njest.replaceProperty(settings, 'timeout', 5000);","handlingStrategy":"type-guard","validationCode":"if (target == null || (typeof target !== 'object' && typeof target !== 'function')) {\n  throw new TypeError(`replaceProperty target must be object/function, got ${typeof target}`);\n}\njest.replaceProperty(target, key, value);","typeGuard":"const isReplaceable = (x: unknown): x is object =>\n  x != null && (typeof x === 'object' || typeof x === 'function');","tryCatchPattern":null,"preventionTips":["Confirm the import resolves to an object before replacing.","Prefer module namespace imports for stable references.","Validate the target type in a shared test-util helper."],"tags":["mock","replace-property","type-validation"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}