I have a series of functions, each performing various firestore interactions. How do I use Jest to mock these firestore calls? I would like to avoid using a library.
If mocking seems tedious, don't. Use the emulators.
I believe this is a relatively new option for handling reads & writes in testing, so I'm posting it. Here's a quick walk-through.
$ curl -sL firebase.tools | bash
$ firebase init
const db = firebase.initializeApp(config).firestore()
if (location.hostname === "localhost") {
db.settings({
host: "localhost:8080",
ssl: false
});
}
$ firebase emulators:start
describe('New city', () => {
it('should create a new city in firestore', async () => {
await db.collection('cities').doc('Seattle').set({ state: "WA" })
const city = await db.collection('cities').doc("Seattle").get()
expect(city.data()['population']).toEqual("WA")
})
})
async function cleanFirestore() {
const Http = new XMLHttpRequest();
const url = "http://localhost:8080/emulator/v1/projects//databases/(default)/documents"
Http.open("DELETE", url);
Http.send();
return new Promise((resolve, reject) => {
setTimeout(reject, 2000)
Http.onreadystatechange = resolve
})
}
For an emulator walkthrough guide from Google: https://google.dev/pathways/firebase-emulators
Docs: https://firebase.google.com/docs/emulator-suite