Created
June 3, 2020 10:00
-
-
Save sgr-ksmt/b82223675afdd0451fb08a3df12d02e7 to your computer and use it in GitHub Desktop.
Helper functions of Firebase test for Cloud Functions and firestore.rules
This file contains hidden or 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
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