Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active March 1, 2024 21:32
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save katowulf/b5178664e7059928b4921fd85be922e5 to your computer and use it in GitHub Desktop.
Save katowulf/b5178664e7059928b4921fd85be922e5 to your computer and use it in GitHub Desktop.
Example of Firebase emulator unit tests and seed Firestore data
{
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"emulators": {
"firestore": {
"port": 8080
}
}
}
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match rows/{rowId} {
allow read: if request.auth.uid == resource.data.owner;
}
}
}
{
"rows/foo": {
"owner": "kato", "score": 10
},
"rows/bar": {
"owner": "puf", "score": 20
}
}
/// <reference path='../node_modules/mocha-typescript/globals.d.ts' />
import * as firebase from "@firebase/testing";
/*
* ============
* Setup
* ============
*/
const port = require("./firebase.json").emulators.firestore.port;
const projectId = "gvcassistant-staging";
const coverageUrl = `http://localhost:${port}/emulator/v1/projects/${projectId}:ruleCoverage.html`;
const fs = require("fs");
const rules = fs.readFileSync("./firestore.rules", "utf8");
const seed = require('./seed.json');
/**
* Creates a new app with authentication data matching the input.
*
* @param {object} auth the object to use for authentication (typically {uid: some-uid})
* @return {object} the app.
*/
function authedApp(auth: object) {
return firebase
.initializeTestApp({ projectId, auth })
.firestore();
}
async function resetData() {
await firebase.clearFirestoreData({ projectId });
const db = firebase.initializeAdminApp({projectId}).firestore();
const promises = Object.entries(seed).map(entry => db.doc(entry[0]).set(entry[1]));
await Promise.all(promises);
}
/*
* ============
* Test Cases
* ============
*/
before(async () => {
await firebase.loadFirestoreRules({ projectId, rules });
});
beforeEach(resetData);
after(async () => {
await Promise.all(firebase.apps().map(app => app.delete()));
console.log(`View rule coverage information at ${coverageUrl}\n`);
});
@suite
class MyApp {
@test
async "can access rows I own"() {
const db = authedApp({uid: "kato"});
const doc = db.doc("rows/foo");
await firebase.assertSucceeds(doc.get());
}
@test
async "cannot access rows I do not own"() {
const db = authedApp({uid: "kato"});
const doc = db.doc("rows/bar");
await firebase.assertFails(doc.get());
}
}
@markgoho
Copy link

markgoho commented Sep 9, 2020

@katowulf are you missing the import statement for the seed.json file?

@katowulf
Copy link
Author

👍

@Dwiga
Copy link

Dwiga commented May 20, 2021

Hi, I have an issue related to firebase auth emulator.. I want to use firebase auth emulator as part of selenium test.. when I tried to test sign up and sign in, I got an error and I think it's related to email verification.. do you know how to automatically turn email_verified to true after a sign up? thank you in advance

@katowulf
Copy link
Author

katowulf commented May 20, 2021

@Dwiga your question is not related to this example, which covers Firestore unit tests in the emulator. Try Stack Overflow or Reddit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment