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

Can't mock firestore via firebase-admin #105

Open
uttrasey opened this issue May 19, 2021 · 5 comments
Open

Can't mock firestore via firebase-admin #105

uttrasey opened this issue May 19, 2021 · 5 comments

Comments

@uttrasey
Copy link

Description

Struggling to get a test passing

Steps to reproduce

const { mockFirebase } = require("firestore-jest-mock");

mockFirebase({
  database: {
    users: [
      { id: "abc123", name: "Homer Simpson" },
      { id: "abc456", name: "Lisa Simpson" },
    ],
    posts: [{ id: "123abc", title: "Really cool title" }],
  },
});

const { mockCollection } = require("firestore-jest-mock/mocks/firestore");

test("testing stuff", () => {
  const admin = require("firebase-admin"); // or import firebase from 'firebase';
  admin.initializeApp();
  const db = admin.firestore();

  return db
    .collection("users")
    .get()
    .then((userDocs) => {
      console.log(userDocs);
      expect(mockCollection).toHaveBeenCalledWith("users");
      expect(userDocs.docs[0].data().name).toEqual("Homer Simpson");
    });
});

Expected result

Test pass

Actual result

Fails, firestore not mocked

Screenshot 2021-05-19 at 15 34 45

Environment

  • Node version:
@kevb10
Copy link

kevb10 commented Sep 17, 2021

same here..watching

@jayesh-remo
Copy link

any updates here?

@sbatson5
Copy link
Owner

Are you all calling mockFirebase outside of a describe block? i.e. is it at the top of the file before your tests are written? The hoisting in jest is weird and it requires that mockFirebase be in a describe block that wraps your tests. See the examples in our repo:
https://github.com/Upstatement/firestore-jest-mock/blob/master/__tests__/auth.test.js#L16-L17

If you have an example repo you can point me to, I'm happy to play around

@jdziek
Copy link

jdziek commented Apr 22, 2022

Hi, sorry for jumping onto that thread like that but you touched on a certain problem im having @sbatson5 with the comment. Can I have multiple "mockFirebase" instances per file im testing in, due to having multiple "describe" blocks? Very often i noticed that the "mockFirebase" conflict with each other even in between of the describes. Mind you, that im using es6 with node 16 which apparently stil doesnt have full support for some mocks and clears in jest annoyingly.

@sbatson5
Copy link
Owner

Hi, sorry for jumping onto that thread like that but you touched on a certain problem im having @sbatson5 with the comment. Can I have multiple "mockFirebase" instances per file im testing in, due to having multiple "describe" blocks? Very often i noticed that the "mockFirebase" conflict with each other even in between of the describes. Mind you, that im using es6 with node 16 which apparently stil doesnt have full support for some mocks and clears in jest annoyingly.

@jdziek Hmm, yeah I would assume that would work but I've also had issues on the latest node and clearing mocks but haven't had time to dig into it. Honestly, jest's hoisting and inconsistent clearing has been one of the biggest headaches for writing such a large mock class, to the point where I just started writing more but smaller test files

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

5 participants