Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beforeEach clearAllMocks not working #103

Closed
jdziek opened this issue May 17, 2021 · 2 comments
Closed

beforeEach clearAllMocks not working #103

jdziek opened this issue May 17, 2021 · 2 comments

Comments

@jdziek
Copy link

jdziek commented May 17, 2021

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
Copy link
Owner

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
Copy link
Author

jdziek commented Jul 23, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants