-
Notifications
You must be signed in to change notification settings - Fork 96
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
Best method for "clearing/resetting" firestore between tests? #119
Comments
It looks like this repo is no longer maintained? |
Just reset the firestore mock using beforeEach. See https://github.com/soumak77/firebase-mock/blob/master/tutorials/integration/setup.md for setup.
Hope this helps :) |
@Jank1310, that doesn't work for Jest tests, though. I have this at the top of my tests: jest.mock('firebaseConfig', () => {
const firebasemock = require('firebase-mock');
const mockauth = new firebasemock.MockAuthentication();
const mockfirestore = new firebasemock.MockFirestore();
return new firebasemock.MockFirebaseSdk(
null, // RTDB
() => mockauth,
() => mockfirestore
);
}); It has to be outside of the beforeEach block to work, so resetting it that way won't work. It would be nice to have some form of cleanup method you could call to quickly clear the data. |
@p12y It works. I'm using it every day :) Maybe I'm missing something, do you see any problems with the following approach? I pass the admin sdk (and other dependencies) into the functions from the index.js. Sample Function
Sample Test
|
@Jank1310 I'm doing integration testing with React, so I need to call // Outside the test block
jest.mock('firebaseConfig', () => {
const firebasemock = require('firebase-mock');
let mockauth = new firebasemock.MockAuthentication();
let mockfirestore = new firebasemock.MockFirestore();
function reset() {
mockauth = new firebasemock.MockAuthentication();
mockfirestore = new firebasemock.MockFirestore();
}
const sdk = new firebasemock.MockFirebaseSdk(
null, // RTDB
() => mockauth,
() => mockfirestore
);
return {
reset,
...sdk,
}
});
// Inside the test block
afterEach(() => {
firebase.reset();
}); Cheers for the help! |
@p12y Below is the simplified code. is there something wrong? mockTarget.js export const counter = { count: 0 }
// for enable import { increment } from './mockTarget'
export const increment = () => {} sample.test.js import { counter, increment } from './mockTarget'
jest.mock('./mockTarget', () => {
let count = 0
return {
counter: count,
increment: () => {
count++
return count
}
}
})
it("simple test", () => {
console.log(counter) // 0
const result = increment()
console.log(result) // 1
console.log(counter) // if incremnet function work, expected 1 but actual 0
expect(true).toBe(true)
})
|
@Ryomasao I'm not sure how that's the same problem? Either way, the reason that's not working is because primitives are passed by value. What you want to do is increment the value of counter, not count.
|
@p12y wrong jest.mock('../firebase',() => {
const firebaseMock = require('firebase-mock')
const mockfirestore = new firebaseMock.MockFirestore()
mockfirestore.autoFlush()
return {
db:mockfirestore,
reset: () => {
// This will not change the db reference
mockfirestore = new firebaseMock.MockFirestore()
mockfirestore.autoFlush()
} }
}) success jest.mock('../firebase',() => {
const firebaseMock = require('firebase-mock')
const mockfirestore = new firebaseMock.MockFirestore()
mockfirestore.autoFlush()
return {
db:mockfirestore,
reset: function(this: any) {
const mockfirestore = new firebaseMock.MockFirestore()
mockfirestore.autoFlush()
this.db = mockfirestore
} }
}) |
Been trying to make this "reset firestore" function work like you did but it's not clearing the firestore database after running 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()
};
}); Sorry for commenting in an old thread but I couldn't find any resource to get this to work, maybe we have a function to reset the database now ? |
Do I just need to cycle through and call
.delete()
for all documents between tests, or is there a better way?I'm using Jest as the test runner.
The text was updated successfully, but these errors were encountered: