TIL... About jest.resetAllMocks() safety

2019-02-27

I was extending some test cases, and found I needed to ensure the mocks weren't polluted between test cases. Pretty standard stuff right?

So I threw a reset into my WHEN so that each assertion could avoid pollution with the others. Here's what I did:

1
2
3
4
5
6
7
describe('WHEN blah',()=>{ 

beforeEach(()=>{/* do some test step*/ jest.resetAllMocks();});
describe('THEN some assertion',()=>{
...
});
};

Did you see it? I didn't either, at first. I was flumuxed when my more tests started failing than before. Eventually I realized my error. I had put the reset AFTER my action step. I was resetting the Mocks before they coudl be checked.

Needless to say there were several facepalm moments that day.