0

I have the following module in my codebase:

const getSomething = () => new SomeThirdPartyCall();

const doSomethingElse = () => {
  const foo = getSomething();
  foo.bar();
};

const someService = { getSomething, doSomethingElse };

export { somethingService }; 

The way export works is to preserve service name in the calls, i.e. somethingService.doSomethingElse call will always be consistent across the app.

Now I'm trying to test doSomethingElse method and mocking getSomething to avoid calling 3rd party lib but it just doesn't work and still calls that 3rd party. What I tried:

somethingService.doSomethingElse = jest.fn().mockReturnValueOnce(); // doesnt work

const doSomethingElseMock = jest.spyOn(somethingService, 'doSomethingElse');
doSomethingElseMock.mockReturnValueOnce() // doesnt work
doSomethingElseMock.mockImplementationOnce() // doesnt work

What did I miss and why it's not mocking the function?

JamesJGoodwin
  • 350
  • 1
  • 3
  • 17
  • Does this answer your question? [How to mock functions in the same module using Jest?](https://stackoverflow.com/questions/45111198/how-to-mock-functions-in-the-same-module-using-jest) – jonrsharpe Aug 12 '22 at 14:42
  • _(Specifically https://stackoverflow.com/a/70066090/3001761, which is to say: don't.)_ – jonrsharpe Aug 12 '22 at 14:42

0 Answers0