Last active
March 1, 2024 21:32
-
-
Save katowulf/b5178664e7059928b4921fd85be922e5 to your computer and use it in GitHub Desktop.
Example of Firebase emulator unit tests and seed Firestore data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"firestore": { | |
"rules": "firestore.rules", | |
"indexes": "firestore.indexes.json" | |
}, | |
"emulators": { | |
"firestore": { | |
"port": 8080 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rules_version = '2'; | |
service cloud.firestore { | |
match /databases/{database}/documents { | |
match rows/{rowId} { | |
allow read: if request.auth.uid == resource.data.owner; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"rows/foo": { | |
"owner": "kato", "score": 10 | |
}, | |
"rows/bar": { | |
"owner": "puf", "score": 20 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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()); | |
} | |
} |
👍
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
@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
@katowulf are you missing the import statement for the
seed.json
file?