Skip to content

Commit bcbb320

Browse files
kevmo314laurenzlong
authored andcommitted
Warn when GCLOUD_PROJECT env variable does not exist (#200)
1 parent 5a4affd commit bcbb320

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

spec/providers/firestore.spec.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ describe('Firestore Functions', () => {
3333
};
3434
};
3535

36-
before(() => {
36+
beforeEach(() => {
3737
process.env.GCLOUD_PROJECT = 'project1';
3838
process.env.FIREBASE_PROJECT = JSON.stringify({
3939
databaseUrl: '[email protected]',
4040
});
4141
});
4242

43-
after(() => {
43+
afterEach(() => {
4444
delete process.env.GCLOUD_PROJECT;
45+
delete process.env.FIREBASE_PROJECT;
4546
});
4647

4748
describe('document builders and event types', () => {
@@ -60,6 +61,11 @@ describe('Firestore Functions', () => {
6061
expect(cloudFunction.__trigger).to.deep.equal(expectedTrigger(resource, 'document.write'));
6162
});
6263

64+
it('should throw useful error when GCLOUD_PROJECT missing', () => {
65+
delete process.env.GCLOUD_PROJECT;
66+
expect(() => firestore.document('users/{uid}')).to.throw(Error, 'GCLOUD_PROJECT');
67+
});
68+
6369
it('should allow custom namespaces', () => {
6470
let resource = 'projects/project1/databases/(default)/documents@v2/users/{uid}';
6571
let cloudFunction = firestore.namespace('v2').document('users/{uid}').onWrite(() => null);

src/providers/firestore.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export const defaultDatabase = '(default)';
3535
let firestoreInstance;
3636

3737
export function database(database: string = defaultDatabase) {
38+
if (!process.env.GCLOUD_PROJECT) {
39+
throw new Error('Environment variable GCLOUD_PROJECT is not set.');
40+
}
3841
return new DatabaseBuilder(posix.join('projects', process.env.GCLOUD_PROJECT, 'databases', database));
3942
}
4043

0 commit comments

Comments
 (0)