-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathutils.ts
More file actions
105 lines (94 loc) · 2.57 KB
/
utils.ts
File metadata and controls
105 lines (94 loc) · 2.57 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { type FirebaseApp, FirebaseError, initializeApp } from "firebase/app";
import { type Auth, connectAuthEmulator, getAuth } from "firebase/auth";
import {
connectDataConnectEmulator,
getDataConnect,
} from "firebase/data-connect";
import {
connectFirestoreEmulator,
type Firestore,
getFirestore,
} from "firebase/firestore";
import { expect } from "vitest";
import { connectorConfig } from "@/dataconnect/default-connector";
const firebaseTestingOptions = {
projectId: "demo-test-project",
apiKey: "test-api-key",
authDomain: "test-auth-domain",
};
let firebaseApp: FirebaseApp | undefined;
let firestore: Firestore;
let auth: Auth;
if (!firebaseApp) {
firebaseApp = initializeApp(firebaseTestingOptions);
firestore = getFirestore(firebaseApp);
auth = getAuth(firebaseApp);
connectFirestoreEmulator(firestore, "localhost", 8080);
connectAuthEmulator(auth, "http://localhost:9099");
connectDataConnectEmulator(
getDataConnect(connectorConfig),
"localhost",
9399,
);
}
async function wipeFirestore() {
const response = await fetch(
"http://localhost:8080/emulator/v1/projects/demo-test-project/databases/(default)/documents",
{
method: "DELETE",
},
);
if (!response.ok) {
throw new Error("Failed to wipe firestore");
}
}
async function wipeAuth() {
const response = await fetch(
"http://localhost:9099/emulator/v1/projects/demo-test-project/accounts",
{
method: "DELETE",
},
);
if (!response.ok) {
throw new Error("Failed to wipe auth");
}
}
function expectFirestoreError(error: unknown, expectedCode: string) {
if (error instanceof FirebaseError) {
expect(error).toBeDefined();
expect(error.code).toBeDefined();
expect(error.code).toBe(expectedCode);
} else {
throw new Error(
"Expected a Firestore error, but received a different type.",
);
}
}
function expectFirebaseError(error: unknown, expectedCode: string) {
if (error instanceof FirebaseError) {
expect(error).toBeDefined();
expect(error.code).toBeDefined();
expect(error.code).toBe(expectedCode);
} else {
console.error("Expected a Firebase error, but received a different type.", {
receivedType: typeof error,
errorDetails:
error instanceof Error
? { message: error.message, stack: error.stack }
: error,
});
throw new Error(
"Expected a Firebase error, but received a different type.",
);
}
}
export {
firestore,
wipeFirestore,
expectFirestoreError,
firebaseTestingOptions,
auth,
wipeAuth,
firebaseApp,
expectFirebaseError,
};