Skip to content

Instantly share code, notes, and snippets.

@sgr-ksmt
Created June 3, 2020 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgr-ksmt/b82223675afdd0451fb08a3df12d02e7 to your computer and use it in GitHub Desktop.
Save sgr-ksmt/b82223675afdd0451fb08a3df12d02e7 to your computer and use it in GitHub Desktop.
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