sbatson5 / firestore-jest-mock

Jest Helper library for mocking Cloud Firestore
https://www.npmjs.com/package/firestore-jest-mock
177 stars 58 forks source link

beforeEach clearAllMocks not working #103

Closed jdziek closed 3 years ago

jdziek commented 3 years ago

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.

describe('getDetails', () => {
  let spyCon = spyConsole();

  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('returns formatted details', async () => {

    mockFirebase({
      database: {
        collection: [
          {
            testingValOne: 'hello',
            testingValTwo: 'world',

          },
        ],
      },
    });
    const firebase = require('firebase'); 
    const db = firebase.firestore();

    const result = await  handleInfo.getDetails(db, mockAuth);

    expect(mockWhere).toHaveBeenCalledWith('payerID', '==', '123');
    expect(result).toEqual({
      testingValOne: 'hello',
      testingValTwo: 'world',
    });
  });

  it('logs out a 404 error', async () => {
    mockFirebase({
      database: {
        collection: [],
      },
    });

    const firebase = require('firebase'); 
    const db = firebase.firestore();

    await handleInfo.getDetails(db, mockAuth);

    expect(console.error).toHaveBeenCalledWith('404 resource not found');
  });

});

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.

sbatson5 commented 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.

jdziek commented 3 years ago

That's brilliant. Thank you very much. That solved a lot of our problems.