Skip to content

Commit ae5a762

Browse files
authored
Fixing a test failure caused by Firestore 0.14.1 release (#279)
1 parent 44c1d10 commit ae5a762

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/firestore/firestore.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class FirestoreService implements FirebaseServiceInterface {
6363
}
6464
}
6565

66-
function initFirestore(app: FirebaseApp): Firestore {
66+
export function getFirestoreOptions(app: FirebaseApp): any {
6767
if (!validator.isNonNullObject(app) || !('options' in app)) {
6868
throw new FirebaseFirestoreError({
6969
code: 'invalid-argument',
@@ -73,7 +73,6 @@ function initFirestore(app: FirebaseApp): Firestore {
7373

7474
const projectId: string = utils.getProjectId(app);
7575
const cert: Certificate = app.options.credential.getCertificate();
76-
let options: any;
7776
if (cert != null) {
7877
// cert is available when the SDK has been initialized with a service account JSON file,
7978
// or by setting the GOOGLE_APPLICATION_CREDENTIALS envrionment variable.
@@ -87,7 +86,7 @@ function initFirestore(app: FirebaseApp): Firestore {
8786
+ 'Alternatively set the GCLOUD_PROJECT environment variable.',
8887
});
8988
}
90-
options = {
89+
return {
9190
credentials: {
9291
private_key: cert.privateKey,
9392
client_email: cert.clientEmail,
@@ -98,17 +97,20 @@ function initFirestore(app: FirebaseApp): Firestore {
9897
// Try to use the Google application default credentials.
9998
// If an explicit project ID is not available, let Firestore client discover one from the
10099
// environment. This prevents the users from having to set GCLOUD_PROJECT in GCP runtimes.
101-
options = validator.isNonEmptyString(projectId) ? {projectId} : {};
102-
} else {
103-
throw new FirebaseFirestoreError({
104-
code: 'invalid-credential',
105-
message: 'Failed to initialize Google Cloud Firestore client with the available credentials. ' +
106-
'Must initialize the SDK with a certificate credential or application default credentials ' +
107-
'to use Cloud Firestore API.',
108-
});
100+
return validator.isNonEmptyString(projectId) ? {projectId} : {};
109101
}
110102

103+
throw new FirebaseFirestoreError({
104+
code: 'invalid-credential',
105+
message: 'Failed to initialize Google Cloud Firestore client with the available credentials. ' +
106+
'Must initialize the SDK with a certificate credential or application default credentials ' +
107+
'to use Cloud Firestore API.',
108+
});
109+
}
110+
111+
function initFirestore(app: FirebaseApp): Firestore {
112+
const options = getFirestoreOptions(app);
111113
// Lazy-load the Firestore implementation here, which in turns loads gRPC.
112-
const firestoreDatabase = require('@google-cloud/firestore');
114+
const firestoreDatabase: typeof Firestore = require('@google-cloud/firestore');
113115
return new firestoreDatabase(options);
114116
}

test/unit/firestore/firestore.spec.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {expect} from 'chai';
2323
import * as mocks from '../../resources/mocks';
2424
import {FirebaseApp} from '../../../src/firebase-app';
2525
import {ApplicationDefaultCredential} from '../../../src/auth/credential';
26-
import {FirestoreService} from '../../../src/firestore/firestore';
26+
import {FirestoreService, getFirestoreOptions} from '../../../src/firestore/firestore';
2727

2828
describe('Firestore', () => {
2929
let mockApp: FirebaseApp;
@@ -142,17 +142,20 @@ describe('Firestore', () => {
142142

143143
describe('client.projectId', () => {
144144
it('should return a string when project ID is present in credential', () => {
145-
expect(firestore.client.projectId).to.equal('project_id');
145+
const options = getFirestoreOptions(mockApp);
146+
expect(options.projectId).to.equal('project_id');
146147
});
147148

148149
it('should return a string when project ID is present in app options', () => {
149-
expect((new FirestoreService(projectIdApp).client as any).projectId).to.equal('explicit-project-id');
150+
const options = getFirestoreOptions(projectIdApp);
151+
expect(options.projectId).to.equal('explicit-project-id');
150152
});
151153

152154
it('should return a string when project ID is present in environment', () => {
153155
process.env.GCLOUD_PROJECT = 'env-project-id';
154156
process.env.GOOGLE_APPLICATION_CREDENTIALS = mockServiceAccount;
155-
expect((new FirestoreService(defaultCredentialApp).client as any).projectId).to.equal('env-project-id');
157+
const options = getFirestoreOptions(defaultCredentialApp);
158+
expect(options.projectId).to.equal('env-project-id');
156159
});
157160
});
158161
});

0 commit comments

Comments
 (0)