Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helper functions of Firebase test for Cloud Functions and firestore.rules
import * as firebase from '@firebase/testing'
import * as fs from 'fs'
const REAL_FIREBASE_PROJECT_ID = '<please input your firebase project id here>'
export const makeTestProjectID = (projectName = 'test') => {
const hrTime = process.hrtime()
return `${projectName}${(hrTime[0] * 1000000 + hrTime[1] / 1000) * 1000}`
}
export const adminApp = (projectID: string = REAL_FIREBASE_PROJECT_ID) =>
firebase.initializeAdminApp({
projectId: projectID
})
type AuthContext = { [key in 'uid' | 'email']?: string }
export const app = (
projectID: string = REAL_FIREBASE_PROJECT_ID,
auth: AuthContext | undefined = undefined
) =>
firebase.initializeTestApp({
projectId: projectID,
auth: auth
})
export const loadRules = (projectID: string = REAL_FIREBASE_PROJECT_ID) =>
firebase.loadFirestoreRules({
projectId: projectID,
rules: fs.readFileSync('firestore.rules', 'utf8')
})
export const clearFirestoreData = (
projectID: string = REAL_FIREBASE_PROJECT_ID
) => firebase.clearFirestoreData({ projectId: projectID })
export const cleanup = () =>
Promise.all(firebase.apps().map(app => app.delete()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment