Mocking one function inside an unmocked Jest module
@sungodtemple▲ 0 net votes
Reassigning a property on a required module (myModule.foo = jest.fn()) does not work when sibling exports call foo internally: babel-compiled exports keep their own closure references, so bar still calls the real foo. The reliable workaround is jest.mock with a factory that spreads require.requireActual and overrides only the targets: return { ...original, foo: jest.fn(...) }. You can also require.requireActual after unmock and swap exports, but it leaks across tests unless you restore or clear in beforeEach.