Skip to content

fix(firestore): use hostname from env variable #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions spec/providers/firestore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import { expect } from 'chai';
import * as firebase from 'firebase-admin';
import * as sinon from 'sinon';
import * as http from 'http';
import { FeaturesList } from '../../src/features';
import fft = require('../../src/index');

describe('providers/firestore', () => {
let test: FeaturesList;
let fakeHttpRequestMethod;
let fakeHttpResponse;

beforeEach(() => {
test = fft();
fakeHttpResponse = {
statusCode: 200,
on: ((event, cb) => cb())
};
fakeHttpRequestMethod = sinon.fake((config, cb) => {
cb(fakeHttpResponse);
});
sinon.replace(http, 'request', fakeHttpRequestMethod);
});

afterEach(() => {
sinon.restore();
});

it('produces the right snapshot with makeDocumentSnapshot', async () => {
Expand Down Expand Up @@ -71,4 +87,30 @@ describe('providers/firestore', () => {
);
expect(snapshot.data().ref.toString()).to.equal(ref.toString());
});

it('should use host name from FIRESTORE_EMULATOR_HOST env in clearFirestoreData', async () => {
process.env.FIRESTORE_EMULATOR_HOST = 'not-local-host:8080';

await test.firestore.clearFirestoreData({projectId: 'not-a-project'});

expect(fakeHttpRequestMethod.calledOnceWith({
hostname: 'not-local-host',
method: 'DELETE',
path: '/emulator/v1/projects/not-a-project/databases/(default)/documents',
port: '8080'
})).to.be.true;
});

it('should use host name from FIREBASE_FIRESTORE_EMULATOR_ADDRESS env in clearFirestoreData', async () => {
process.env.FIREBASE_FIRESTORE_EMULATOR_ADDRESS = 'custom-host:9090';

await test.firestore.clearFirestoreData({projectId: 'not-a-project'});

expect(fakeHttpRequestMethod.calledOnceWith({
hostname: 'custom-host',
method: 'DELETE',
path: '/emulator/v1/projects/not-a-project/databases/(default)/documents',
port: '9090'
})).to.be.true;
});
});
15 changes: 7 additions & 8 deletions src/providers/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,14 @@ const FIRESTORE_ADDRESS_ENVS = [
'FIREBASE_FIRESTORE_EMULATOR_ADDRESS',
];

const FIRESTORE_ADDRESS = FIRESTORE_ADDRESS_ENVS.reduce(
(addr, name) => process.env[name] || addr,
'localhost:8080'
);
const FIRESTORE_PORT = FIRESTORE_ADDRESS.split(':')[1];

/** Clears all data in firestore. Works only in offline mode.
*/
export function clearFirestoreData(options: { projectId: string } | string) {
const FIRESTORE_ADDRESS = FIRESTORE_ADDRESS_ENVS.reduce(
(addr, name) => process.env[name] || addr,
'localhost:8080'
);

return new Promise((resolve, reject) => {
let projectId;

Expand All @@ -272,8 +271,8 @@ export function clearFirestoreData(options: { projectId: string } | string) {

const config = {
method: 'DELETE',
hostname: 'localhost',
port: FIRESTORE_PORT,
hostname: FIRESTORE_ADDRESS.split(':')[0],
port: FIRESTORE_ADDRESS.split(':')[1],
path: `/emulator/v1/projects/${projectId}/databases/(default)/documents`,
};

Expand Down