Skip to content

Commit 56287f4

Browse files
authored
fix(firestore): use hostname from env variable (#184)
1 parent 53f3e3e commit 56287f4

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

spec/providers/firestore.spec.ts

+42
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/features';
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,30 @@ 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(fakeHttpRequestMethod.calledOnceWith({
97+
hostname: 'not-local-host',
98+
method: 'DELETE',
99+
path: '/emulator/v1/projects/not-a-project/databases/(default)/documents',
100+
port: '8080'
101+
})).to.be.true;
102+
});
103+
104+
it('should use host name from FIREBASE_FIRESTORE_EMULATOR_ADDRESS env in clearFirestoreData', async () => {
105+
process.env.FIREBASE_FIRESTORE_EMULATOR_ADDRESS = 'custom-host:9090';
106+
107+
await test.firestore.clearFirestoreData({projectId: 'not-a-project'});
108+
109+
expect(fakeHttpRequestMethod.calledOnceWith({
110+
hostname: 'custom-host',
111+
method: 'DELETE',
112+
path: '/emulator/v1/projects/not-a-project/databases/(default)/documents',
113+
port: '9090'
114+
})).to.be.true;
115+
});
74116
});

src/providers/firestore.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,14 @@ const FIRESTORE_ADDRESS_ENVS = [
255255
'FIREBASE_FIRESTORE_EMULATOR_ADDRESS',
256256
];
257257

258-
const FIRESTORE_ADDRESS = FIRESTORE_ADDRESS_ENVS.reduce(
259-
(addr, name) => process.env[name] || addr,
260-
'localhost:8080'
261-
);
262-
const FIRESTORE_PORT = FIRESTORE_ADDRESS.split(':')[1];
263-
264258
/** Clears all data in firestore. Works only in offline mode.
265259
*/
266260
export function clearFirestoreData(options: { projectId: string } | string) {
261+
const FIRESTORE_ADDRESS = FIRESTORE_ADDRESS_ENVS.reduce(
262+
(addr, name) => process.env[name] || addr,
263+
'localhost:8080'
264+
);
265+
267266
return new Promise((resolve, reject) => {
268267
let projectId;
269268

@@ -277,8 +276,8 @@ export function clearFirestoreData(options: { projectId: string } | string) {
277276

278277
const config = {
279278
method: 'DELETE',
280-
hostname: 'localhost',
281-
port: FIRESTORE_PORT,
279+
hostname: FIRESTORE_ADDRESS.split(':')[0],
280+
port: FIRESTORE_ADDRESS.split(':')[1],
282281
path: `/emulator/v1/projects/${projectId}/databases/(default)/documents`,
283282
};
284283

0 commit comments

Comments
 (0)