How can you spy on a function that has been exported, but also called in a function like this:
File 1:
export function myFunction(){
// do stuff
return myOtherFunction()
}
export function myOtherFunction(){
// do stuff
}
File 2:
import * as helpers from './helpers'
beforeEach(()=>{
spyOn(helpers, 'myOtherFunction');
});
it('should call myOtherFunction', ()=>{
helpers.myFunction();
expect(helpers.myOtherFunction).toHaveBeenCalled()
});
The problem with this is that myOtherFunction
will never be called, because jasmine is spying on the wrong object. myFunction
will call the local function and the jasmine test will expect the exported variable to get called, they are different objects.