Closed jdziek closed 3 years ago
Which leads me to another question. How to handle a situation where the logic is dependent on the response of the "where" query?
Hey @Foxford13 -- sorry for the long delay in a response. But as to your question here, we now have an optional flag you can pass to mockFirebase
which allows for querying:
https://github.com/Upstatement/firestore-jest-mock#simulatequeryfilters
This was mainly put in place so users could test queries that didn't return any results. However, be mindful of writing assertions around mockWhere
. If you are asserting that your where
clause is properly filtering, you'll only be testing our mocked version of where
and not the query itself.
That's brilliant. Thank you very much. That solved a lot of our problems.
Description
Basically what the title says. I have two tests and when i test them separately they have correct values, but when i run therm in bulk mockFirebase() with the values from the test above go to the one below despite the clearAllMocks method. Describe the issue that you're seeing.
The mock data from 'test returns true' gets passed to 'logs out a 404 error' where im trying to mock an empty collection.
Not sure if its the issue with jest, or this module to be honest, but this module being out of the norm i though ill ask here.
EDIT: managed to get it to get it to work with afterEach(()=> {jest.resetAllModules()}), though im not so sure about this solution if the tests potentially will have to share them.
EDIT2: Yeah, that was very shortlived. As soon as i started adding different tests and mocks that broke. The particularly temperamental ones are mockUpdate, mockDoc, mockCollection etc. Even clearing them directly with mockClear() doesnt work
EDIT3: ok, managed to get it to work after the last attempt and moving out the mockFirebase and db beyond the scope of describe fixed it with beforeEaach(()=>jest.clearAllMocks()) working. Even though I did it differently than in the example provided (mocking db in each test),which should be an obvious clue. there was a good reason for it. The reason being that mocked where doesnt filter the response from the mocked db and some of the logic depended on the response from the db which naturally made me think that mocking db in each test with different data would solve the problem. Also, each test, when run separately, worked. Just when running them all together the mocked values werent getting reset. Which leads me to another question. How to handle a situation where the logic is dependent on the response of the "where" query? Clearly mocking firestore in each test doesnt work.