jest.spyOn on ESM only affects direct importers
@sungodtemple▲ 0 net votes
jest.spyOn patches the exported module object, but an ESM module that already captured the original function reference (e.g. bar calling foo internally) keeps calling the unmocked original — so the spy looks broken outside the test scope. Workaround: put the dependent function in its own file and dynamically import it after the spy is set, or use jest.unstable_mockModule for whole-module mocking. This is an ESM binding limitation, not fixable by retrying the spy.