{"id":667,"hash":"98e6aaaaae797cd8e20ce769562fd9fb775fa2525ec180f4debb69c262a69da2","pattern":"jest spyOn not working on index file, cannot redefine property","full_message":"I have UserContext and a hook useUser exported from src/app/context/user-context.tsx.\nAdditionally I have an index.tsx file in src/app/context which exports all child modules.\n\nIf I spyOn src/app/context/user-context it works but changing the import to src/app/context I get:\n\nTypeError: Cannot redefine property: useUser at Function.defineProperty (<anonymous>)\n\nWhy is that?\n\nSource code:\n\n// src/app/context/user-context.tsx\n\nexport const UserContext = React.createContext({});\n\nexport function useUser() {\n  return useContext(UserContext);;\n}\n\n// src/app/context/index.tsx\n\nexport * from \"./user-context\";\n\n// *.spec.tsx\n\n// This works:\nimport * as UserContext from \"src/app/context/user-context\";\n\n// This does not work:\n// import * as UserContext from \"src/app/context\";\n\nit(\"should render complete navigation when user is logged in\", () => {\n\n    jest.spyOn(UserContext, \"useUser\").mockReturnValue({\n        user: mockUser,\n        update: (user) => null,\n        initialized: true,\n    });\n})","ecosystem":"npm","package_name":"reactjs","package_version":null,"solution":"If you take a look at the js code produced for a re-export it looks like this\n\nObject.defineProperty(exports, \"__esModule\", {\n  value: true\n});\n\nvar _context = require(\"context\");\n\nObject.keys(_context).forEach(function (key) {\n  if (key === \"default\" || key === \"__esModule\") return;\n  if (key in exports && exports[key] === _context[key]) return;\n  Object.defineProperty(exports, key, {\n    enumerable: true,\n    get: function get() {\n      return _context[key];\n    }\n  });\n});\n\nand the error you get is due to the compiler not adding configurable: true in the options for defineProperty, which would allow jest to redefine the export to mock it, from docs\n\nconfigurable\n\ntrue if the type of this property descriptor may be changed and if the\nproperty may be deleted from the corresponding object. Defaults to false.\n\nI think you could tweak your config somehow to make the compiler add that, but it all depends on the tooling you're using.\n\nA more accessible approach would be using jest.mock instead of jest.spyOn to mock the user-context file rather than trying to redefine an unconfigurable export\n\nit(\"should render complete navigation when user is logged in\", () => {\n  jest.mock('./user-context', () => {\n    return {\n      ...jest.requireActual('./user-context'),\n      useUser: jest.fn().mockReturnValue({\n        user: {},\n        update: (user: any) => null,\n        initialized: true\n      })\n    }\n  })\n});","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/67872622/jest-spyon-not-working-on-index-file-cannot-redefine-property","votes":115,"created_at":"2026-04-19T04:51:27.269343+00:00","updated_at":"2026-04-19T04:51:27.269343+00:00"}