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

Clear, reset or erase Firestore database in each test #73

Open
dev-andremonteiro opened this issue Jan 12, 2021 · 1 comment
Open

Clear, reset or erase Firestore database in each test #73

dev-andremonteiro opened this issue Jan 12, 2021 · 1 comment

Comments

@dev-andremonteiro
Copy link

I have posted to this issue in the old repository, but it seems like we moved to this repository so I might as well post here, sorry about that I didn't know issues should be moved to this one.

I have been trying to reset my firestore database so my tests don't have remaining data from past tests, I have found some resources in the issue linked above but I can't seem to get the database reset even after making the reassignment of the firestore variable.

jest.mock('../init', () => {
  const firebasemock = require('firebase-mock');

  let mockauth = new firebasemock.MockAuthentication();
  let mockstorage = new firebasemock.MockStorage();
  let mockfirestore = new firebasemock.MockFirestore();
  mockfirestore.autoFlush();

  return {
    reset: function() {
      const mockAut = new firebasemock.MockAuthentication();
      const mockStor = new firebasemock.MockStorage();
      const mockFire = new firebasemock.MockFirestore();
      mockFire.autoFlush();

      this.auth = mockAut;
      this.storage = mockStor;
      this.firestore = mockFire;
    },
    firestore: mockfirestore,
    auth: mockauth,
    storage: mockstorage,
    functions: jest.fn()
  };
});

Maybe some of you know how to do this in version 2.3.2, do we have a function that makes this in the library ?

@alexlouden
Copy link

I'm not sure it's the correct way to do it, but this is what I use:

test utils:

const firestoreDeleteChildren = ref => {
  // Firestore recursive delete
  Object.values(ref.children).forEach(child => {
    firestoreDeleteChildren(child)
    if (child.delete) {
      child.delete()
    }
  })

exports.clearFirestore = () => {
  const { firestore } = require('./pathToYourFirestoreInit')
  // safe!
  if (firestore.constructor.name === 'MockFirestore') {
    console.log('Clearing MockFirestore')
    firestoreDeleteChildren(firestore)
  }
}

in tests:

const { clearFirestore } = './testUtils'

beforeEach(() => {
  clearFirestore()
})

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