If I have code something like this:
import externalThing from 'externalThing'
import anotherThing from 'anotherThing'
function functionIWantToTest()
{
externalThing.doSomething()
.then(data=>{
anotherThing.do(data.Id)
})
}
What is the best practice way to unit test this?
Ideally the test would be something like this:
import externalThing from 'externalThing'
import anotherThing from 'anotherThing'
jest.mock('externalThing',()=>jest.fn())
jest.mock('anotherThing',()=>jest.fn())
describe('when calling functionIWantToTest',()=>{
beforeEach(()=>{
anotherThing.do=jest.fn()
//mock external thing somehow so that it still the 'then' code
functionIWantToTest()
})
it('anotherThing should be called',()=>{
expect(anotherThing.do).toHaveBeenCalledWith(1)
});
});
But when I try I just end up building a chain of mocked functions with jest.fn() and no code actually gets executed.