Skip to content

Commit 48fd59a

Browse files
committed
fix: unskip and fix tests
1 parent a9941d3 commit 48fd59a

File tree

7 files changed

+161
-46
lines changed

7 files changed

+161
-46
lines changed

_emulator/firebase.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"hosting": {
1313
"port": 8081
1414
},
15+
"functions": {
16+
"port": 5001
17+
},
1518
"storage": {
1619
"port": 9199
1720
},

functions/__tests__/bundle.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe("buildQuery", () => {
77
db = admin.initializeApp({ projectId: "demo-experimental" }).firestore();
88
});
99

10-
xit("should build expected queries", () => {
10+
it("should build expected queries", () => {
1111
const queries: [admin.firestore.Query, admin.firestore.Query][] = [
1212
[
1313
buildQuery(db, { collection: "test-coll", conditions: [] }, {}, {}),

functions/__tests__/functions.test.ts

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as admin from "firebase-admin";
2+
import { setupTestData } from "./test-setup";
23

34
process.env.FIRESTORE_EMULATOR_HOST = "localhost:8080";
45
process.env.FIREBASE_FIRESTORE_EMULATOR_ADDRESS = "localhost:8080";
@@ -12,8 +13,14 @@ if (admin.apps.length === 0) {
1213
admin.initializeApp({ projectId: "demo-experimental" });
1314
}
1415

16+
// Setup test data before all tests
17+
beforeAll(async () => {
18+
await setupTestData();
19+
});
20+
1521
const extractObjectfromBuffer = ($) => {
16-
const splitBuffers = $.toString().replace(/[\d]+[{]+/g, ",{");
22+
const buffer = Buffer.from($);
23+
const splitBuffers = buffer.toString().replace(/[\d]+[{]+/g, ",{");
1724
const formatted = splitBuffers.toString().substring(1);
1825
return JSON.parse(`[${formatted.toString()}]`);
1926
};
@@ -29,7 +36,11 @@ describe("functions", () => {
2936
it("successfully returns a bundle with queries, documents and params combined", async () => {
3037
const bundleName = "documents-queries-params";
3138
const url = extUrl(bundleName);
32-
const response = await fetch(url);
39+
const response = await fetch(url, {
40+
headers: {
41+
"Accept-Encoding": "identity",
42+
},
43+
});
3344
const bundle = await response.arrayBuffer();
3445

3546
const [metadata, documentMetadata, document] =
@@ -49,7 +60,11 @@ describe("functions", () => {
4960
it("successfully returns a bundle using a query with a collection", async () => {
5061
const bundleName = "query-with-a-collection";
5162
const url = extUrl(bundleName);
52-
const response = await fetch(url);
63+
const response = await fetch(url, {
64+
headers: {
65+
"Accept-Encoding": "identity",
66+
},
67+
});
5368
const bundle = await response.arrayBuffer();
5469

5570
const [metadata, documentMetadata, document] =
@@ -69,7 +84,11 @@ describe("functions", () => {
6984
it("successfully returns a bundle using a query with a collection and condition", async () => {
7085
const bundleName = "query-with-a-collection-and-condition";
7186
const url = extUrl(bundleName);
72-
const response = await fetch(url);
87+
const response = await fetch(url, {
88+
headers: {
89+
"Accept-Encoding": "identity",
90+
},
91+
});
7392
const bundle = await response.arrayBuffer();
7493

7594
const [metadata, documentMetadata, document] =
@@ -89,7 +108,11 @@ describe("functions", () => {
89108
it("successfully returns a bundle using a query with a collection and where clause", async () => {
90109
const bundleName = "query-with-a-collection-and-condition";
91110
const url = extUrl(bundleName);
92-
const response = await fetch(url);
111+
const response = await fetch(url, {
112+
headers: {
113+
"Accept-Encoding": "identity",
114+
},
115+
});
93116
const bundle = await response.arrayBuffer();
94117

95118
const [metadata, documentMetadata, document] =
@@ -106,10 +129,14 @@ describe("functions", () => {
106129
expect(document.documentMetadata.queries[0]).toEqual("example");
107130
});
108131

109-
xit("successfully returns a bundle using a query with a collection and multiple where clauses", async () => {
132+
it("successfully returns a bundle using a query with a collection and multiple where clauses", async () => {
110133
const bundleName = "query-with-a-collection-and-multiple-where-conditions";
111134
const url = extUrl(bundleName);
112-
const response = await fetch(url);
135+
const response = await fetch(url, {
136+
headers: {
137+
"Accept-Encoding": "identity",
138+
},
139+
});
113140
const bundle = await response.arrayBuffer();
114141

115142
const [metadata, documentMetadata, document] =
@@ -129,7 +156,11 @@ describe("functions", () => {
129156
it("successfully returns a bundle using a document", async () => {
130157
const bundleName = "single-document";
131158
const url = extUrl(bundleName);
132-
const response = await fetch(url);
159+
const response = await fetch(url, {
160+
headers: {
161+
"Accept-Encoding": "identity",
162+
},
163+
});
133164
const bundle = await response.arrayBuffer();
134165

135166
const [metadata, documentMetadata, document] =
@@ -153,7 +184,11 @@ describe("functions", () => {
153184
it("successfully returns a bundle using multiple documents", async () => {
154185
const bundleName = "multiple-documents";
155186
const url = extUrl(bundleName);
156-
const response = await fetch(url);
187+
const response = await fetch(url, {
188+
headers: {
189+
"Accept-Encoding": "identity",
190+
},
191+
});
157192
const bundle = await response.arrayBuffer();
158193

159194
const [metadata, documentMetadata, document] =
@@ -178,7 +213,11 @@ describe("functions", () => {
178213
it("successfully returns a bundle using params", async () => {
179214
const bundleName = "query-with-param";
180215
const url = extUrl(bundleName) + "?name=document2";
181-
const response = await fetch(url);
216+
const response = await fetch(url, {
217+
headers: {
218+
"Accept-Encoding": "identity",
219+
},
220+
});
182221
const bundle = await response.arrayBuffer();
183222

184223
const [metadata, documentMetadata, document] =
@@ -198,7 +237,11 @@ describe("functions", () => {
198237
it("successfully returns a bundle using clientCache", async () => {
199238
const bundleName = "with-client-cache";
200239
const url = extUrl(bundleName);
201-
const response = await fetch(url);
240+
const response = await fetch(url, {
241+
headers: {
242+
"Accept-Encoding": "identity",
243+
},
244+
});
202245
const bundle = await response.arrayBuffer();
203246

204247
const [metadata, documentMetadata, document] =
@@ -209,10 +252,14 @@ describe("functions", () => {
209252
expect(metadata.metadata.totalDocuments).toEqual(0);
210253
});
211254

212-
xit("successfully returns a bundle using serverCache", async () => {
255+
it("successfully returns a bundle using serverCache", async () => {
213256
const bundleName = "with-server-cache";
214257
const url = extUrl(bundleName);
215-
const response = await fetch(url);
258+
const response = await fetch(url, {
259+
headers: {
260+
"Accept-Encoding": "identity",
261+
},
262+
});
216263
const bundle = await response.arrayBuffer();
217264

218265
const [metadata, documentMetadata, document] =
@@ -223,10 +270,14 @@ describe("functions", () => {
223270
expect(metadata.metadata.totalDocuments).toEqual(0);
224271
});
225272

226-
xit("successfully returns a bundle using fileCache", async () => {
273+
it("successfully returns a bundle using fileCache", async () => {
227274
const bundleName = "with-file-cache";
228275
const url = extUrl(bundleName);
229-
const response = await fetch(url);
276+
const response = await fetch(url, {
277+
headers: {
278+
"Accept-Encoding": "identity",
279+
},
280+
});
230281
const bundle = await response.arrayBuffer();
231282

232283
const [metadata, documentMetadata, document] =
@@ -240,7 +291,11 @@ describe("functions", () => {
240291
xit("successfully returns a request through a webiste hosted by Firebase", async () => {
241292
const bundleName = "documents-queries-params";
242293
const url = extHostedUrl(bundleName);
243-
const response = await fetch(url);
294+
const response = await fetch(url, {
295+
headers: {
296+
"Accept-Encoding": "identity",
297+
},
298+
});
244299
const bundle = await response.arrayBuffer();
245300

246301
const [metadata, documentMetadata, document] =
@@ -259,9 +314,13 @@ describe("functions", () => {
259314

260315
it("returns a 404 response if an unknown bundle is provided", async () => {
261316
const bundleName = "unknown-bundle";
262-
const url = extHostedUrl(bundleName);
317+
const url = extUrl(bundleName);
263318

264-
const response = await fetch(url);
319+
const response = await fetch(url, {
320+
headers: {
321+
"Accept-Encoding": "identity",
322+
},
323+
});
265324
expect(response.status).toEqual(404);
266325
});
267326
});

functions/__tests__/test-setup.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import * as admin from "firebase-admin";
2+
3+
// Initialize admin if not already initialized
4+
if (admin.apps.length === 0) {
5+
admin.initializeApp({ projectId: "demo-experimental" });
6+
}
7+
8+
const db = admin.firestore();
9+
10+
export async function setupTestData() {
11+
const bundlesCollection = db.collection("bundles");
12+
13+
// Add missing bundle specs
14+
const missingBundles = [
15+
{
16+
id: "query-with-a-collection-and-multiple-where-conditions",
17+
data: {
18+
queries: {
19+
example: {
20+
collection: "documents",
21+
conditions: [
22+
{ where: ["example", "==", "document"] },
23+
{ where: ["name", "==", "document1"] },
24+
],
25+
},
26+
},
27+
},
28+
},
29+
{
30+
id: "with-server-cache",
31+
data: {
32+
serverCache: 300,
33+
},
34+
},
35+
{
36+
id: "with-file-cache",
37+
data: {
38+
fileCache: true,
39+
},
40+
},
41+
];
42+
43+
const promises = missingBundles.map((bundle) =>
44+
bundlesCollection.doc(bundle.id).set(bundle.data, { merge: true }),
45+
);
46+
47+
await Promise.all(promises);
48+
}

functions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"lint:check": "biome check",
77
"format": "biome format --write",
88
"build": "tsc",
9-
"test": "jest",
9+
"test": "cd ../_emulator && firebase emulators:exec --import=./import --only firestore,functions,storage,extensions --project demo-experimental 'cd ../functions && jest'",
1010
"generate-readme": "firebase ext:info .. --markdown > ../README.md"
1111
},
1212
"main": "lib/index.js",

0 commit comments

Comments
 (0)