clarkbw / jest-localstorage-mock

A module to mock window.localStorage and window.sessionStorage in Jest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"jest.resetAllMocks();" does not work

Oliboy50 opened this issue · comments

capture d ecran 2018-08-22 a 16 28 23

in the README, you say that jest.resetAllMocks works, but it does not, if I use it, the __STORE__ is always empty even if I setItem() in my tests

localStorage.clear(); for instance works as expected

but when I use jest.resetAllMocks (to clear other mocks of my test) in combination to localStorage.clear();, it does not work anymore

I'm running jest v23.4.2

Can you link to the code you're using? I don't have enough context from this to understand the problem.

for instance, you could add a test in this project and try to use jest.resetAllMocks yourself to check if it really does work

if I use it, the STORE is always empty even if I setItem() in my tests

you could add a test in this project and try to use jest.resetAllMocks yourself to check if it really does work

I'll try to take a look. Feel free to create a PR that fails this way.

I had this same issue as well but as per this line I guess .clearAllMocks() is the correct method to use (reference)

I switched to clearAllMocks and now __STORAGE__ seems to be working.

jest.resetAllMocks clears jest.fn() mocked implementations. Because of that all jest.fn() used in localstorage.js will start returning undefined. This is especially annoying if you enable resetMocks in jest config.

I was hoping that adding beforeEach(() => require('jest-localstorage-mock') would solve this issue but that's not a case.

Thanks @daniellizik that worked for me.

More specifically, resetAllMocks breaks the mocks. clearAllMocks clears the mocks (call count, etc) but does not clear the storage. I had to use it in conjunction with localStorage.clear() to fully reset the state between tests.

I'm surprised this has been open for years now, when all that needs to be done is to update the documentation.