TIL... About jest.resetAllMocks()

2018-12-18

This iteration I was writing an Integration style test in Jest. I needed to verify that certain mocks weren't called in the Unhappy Path of the test. But because the Happy Path tests called them, they had 'old' call data in the mock.

After a bit of research I found my answer: tell Jest to reset the mocks. All you have to do is add:

1
2
3
4
5
6
describe('when *your test step*'()=>{
afterEach(()=>{
jest.resetAllMocks();
})
it('then *your validation*',()=>{...});
});

And as the name implies Jest dumps the 'old' call data. And now I can verify my Unhappy Paths.