Skip to content

Commit 9993db0

Browse files
authored
Merge branch 'master' into @invertase/code-adjustments
2 parents 1205233 + a4d2de0 commit 9993db0

File tree

5 files changed

+66
-19
lines changed

5 files changed

+66
-19
lines changed

.github/workflows/test.yaml

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ jobs:
1313
strategy:
1414
matrix:
1515
node-version:
16-
- 14.x
17-
- 16.x
16+
- 20.x
17+
- 22.x
1818
steps:
19-
- uses: actions/checkout@v1
20-
- uses: actions/setup-node@v1
19+
- uses: actions/checkout@v3
20+
- uses: actions/setup-node@v3
2121
with:
2222
node-version: ${{ matrix.node-version }}
2323

@@ -36,11 +36,11 @@ jobs:
3636
strategy:
3737
matrix:
3838
node-version:
39-
- 14.x
40-
- 16.x
39+
- 20.x
40+
- 22.x
4141
steps:
42-
- uses: actions/checkout@v1
43-
- uses: actions/setup-node@v1
42+
- uses: actions/checkout@v3
43+
- uses: actions/setup-node@v3
4444
with:
4545
node-version: ${{ matrix.node-version }}
4646

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "firebase-functions-test",
3-
"version": "3.4.0",
3+
"version": "3.4.1",
44
"description": "A testing companion to firebase-functions.",
55
"main": "lib/index.js",
66
"scripts": {

spec/providers/firestore.spec.ts

+48
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
import { expect } from 'chai';
22
import * as firebase from 'firebase-admin';
3+
import * as sinon from 'sinon';
4+
import * as http from 'http';
35
import { FeaturesList } from '../../src/types/commonTypes';
46
import fft = require('../../src/index');
57

68
describe('providers/firestore', () => {
79
let test: FeaturesList;
10+
let fakeHttpRequestMethod;
11+
let fakeHttpResponse;
812

913
beforeEach(() => {
1014
test = fft();
15+
fakeHttpResponse = {
16+
statusCode: 200,
17+
on: (event, cb) => cb(),
18+
};
19+
fakeHttpRequestMethod = sinon.fake((config, cb) => {
20+
cb(fakeHttpResponse);
21+
});
22+
sinon.replace(http, 'request', fakeHttpRequestMethod);
23+
});
24+
25+
afterEach(() => {
26+
sinon.restore();
1127
});
1228

1329
it('produces the right snapshot with makeDocumentSnapshot', async () => {
@@ -71,4 +87,36 @@ describe('providers/firestore', () => {
7187
);
7288
expect(snapshot.data().ref.toString()).to.equal(ref.toString());
7389
});
90+
91+
it('should use host name from FIRESTORE_EMULATOR_HOST env in clearFirestoreData', async () => {
92+
process.env.FIRESTORE_EMULATOR_HOST = 'not-local-host:8080';
93+
94+
await test.firestore.clearFirestoreData({ projectId: 'not-a-project' });
95+
96+
expect(
97+
fakeHttpRequestMethod.calledOnceWith({
98+
hostname: 'not-local-host',
99+
method: 'DELETE',
100+
path:
101+
'/emulator/v1/projects/not-a-project/databases/(default)/documents',
102+
port: '8080',
103+
})
104+
).to.be.true;
105+
});
106+
107+
it('should use host name from FIREBASE_FIRESTORE_EMULATOR_ADDRESS env in clearFirestoreData', async () => {
108+
process.env.FIREBASE_FIRESTORE_EMULATOR_ADDRESS = 'custom-host:9090';
109+
110+
await test.firestore.clearFirestoreData({ projectId: 'not-a-project' });
111+
112+
expect(
113+
fakeHttpRequestMethod.calledOnceWith({
114+
hostname: 'custom-host',
115+
method: 'DELETE',
116+
path:
117+
'/emulator/v1/projects/not-a-project/databases/(default)/documents',
118+
port: '9090',
119+
})
120+
).to.be.true;
121+
});
74122
});

src/providers/firestore.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,14 @@ const FIRESTORE_ADDRESS_ENVS = [
249249
'FIREBASE_FIRESTORE_EMULATOR_ADDRESS',
250250
];
251251

252-
const FIRESTORE_ADDRESS = FIRESTORE_ADDRESS_ENVS.reduce(
253-
(addr, name) => process.env[name] || addr,
254-
'localhost:8080'
255-
);
256-
const FIRESTORE_PORT = FIRESTORE_ADDRESS.split(':')[1];
257-
258252
/** Clears all data in firestore. Works only in offline mode.
259253
*/
260254
export function clearFirestoreData(options: { projectId: string } | string) {
255+
const FIRESTORE_ADDRESS = FIRESTORE_ADDRESS_ENVS.reduce(
256+
(addr, name) => process.env[name] || addr,
257+
'localhost:8080'
258+
);
259+
261260
return new Promise((resolve, reject) => {
262261
let projectId;
263262

@@ -271,8 +270,8 @@ export function clearFirestoreData(options: { projectId: string } | string) {
271270

272271
const config = {
273272
method: 'DELETE',
274-
hostname: 'localhost',
275-
port: FIRESTORE_PORT,
273+
hostname: FIRESTORE_ADDRESS.split(':')[0],
274+
port: FIRESTORE_ADDRESS.split(':')[1],
276275
path: `/emulator/v1/projects/${projectId}/databases/(default)/documents`,
277276
};
278277

0 commit comments

Comments
 (0)