forked from CodingCatDev/codingcat.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
45 lines (38 loc) · 1.07 KB
/
helpers.ts
File metadata and controls
45 lines (38 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import {
loadFirestoreRules,
initializeTestApp,
clearFirestoreData,
initializeAdminApp,
} from '@firebase/rules-unit-testing';
import firebase from 'firebase/app';
import { readFileSync } from 'fs';
const serviceAccountKeyFile = readFileSync('../serviceAccountKey.json', 'utf8');
const serviceAccountKey = JSON.parse(serviceAccountKeyFile);
export async function setup(auth: any, data: any) {
const projectId = serviceAccountKey.project_id;
const app = initializeTestApp({
projectId,
auth,
});
const db = app.firestore();
// Write mock documents before rules
if (data) {
const admin = initializeAdminApp({
projectId,
});
for (const key in data) {
const ref = admin.firestore().doc(key);
await ref.set(data[key]);
}
}
// Apply rules
await loadFirestoreRules({
projectId,
rules: readFileSync('../firestore.rules', 'utf8'),
});
return db;
}
export async function teardown() {
Promise.all(firebase.apps.map((app) => app.delete()));
await clearFirestoreData({ projectId: serviceAccountKey.project_id });
}