Skip to content

Commit 56bdc81

Browse files
authored
fix: Updated integration tests to use the modular API surface (#1173)
* fix: Updated integration tests to use the modular API surface * fix: Updated integration tests to use new module entry points
1 parent 0ebb759 commit 56bdc81

12 files changed

+529
-436
lines changed

test/integration/app.spec.ts

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616

1717
import * as admin from '../../lib/index';
18+
import { App, deleteApp, getApp, initializeApp } from '../../lib/app/index';
19+
import { getAuth } from '../../lib/auth/index';
1820
import { expect } from 'chai';
1921
import {
2022
defaultApp, nullApp, nonNullApp, databaseUrl, projectId, storageBucket,
@@ -27,30 +29,56 @@ describe('admin', () => {
2729
expect(storageBucket).to.be.not.empty;
2830
});
2931

30-
it('does not load RTDB by default', () => {
31-
const firebaseRtdb = require.cache[require.resolve('@firebase/database')];
32-
expect(firebaseRtdb).to.be.undefined;
33-
const rtdbInternal = require.cache[require.resolve('../../lib/database/database-internal')];
34-
expect(rtdbInternal).to.be.undefined;
35-
});
32+
describe('Dependency lazy loading', () => {
33+
const tempCache: {[key: string]: any} = {};
34+
const dependencies = ['@firebase/database', '@google-cloud/firestore'];
35+
let lazyLoadingApp: App;
3636

37-
it('loads RTDB when calling admin.database', () => {
38-
const rtdbNamespace = admin.database;
39-
expect(rtdbNamespace).to.not.be.null;
40-
const firebaseRtdb = require.cache[require.resolve('@firebase/database')];
41-
expect(firebaseRtdb).to.not.be.undefined;
42-
});
37+
before(() => {
38+
// Unload dependencies if already loaded. Some of the other test files have imports
39+
// to firebase-admin/database and firebase-admin/firestore, which cause the corresponding
40+
// dependencies to get loaded before the tests are executed.
41+
dependencies.forEach((name) => {
42+
const resolvedName = require.resolve(name);
43+
tempCache[name] = require.cache[resolvedName];
44+
delete require.cache[resolvedName];
45+
});
4346

44-
it('does not load Firestore by default', () => {
45-
const gcloud = require.cache[require.resolve('@google-cloud/firestore')];
46-
expect(gcloud).to.be.undefined;
47-
});
47+
// Initialize the SDK
48+
lazyLoadingApp = initializeApp(defaultApp.options, 'lazyLoadingApp');
49+
});
50+
51+
it('does not load RTDB by default', () => {
52+
const firebaseRtdb = require.cache[require.resolve('@firebase/database')];
53+
expect(firebaseRtdb).to.be.undefined;
54+
});
55+
56+
it('loads RTDB when calling admin.database', () => {
57+
const rtdbNamespace = admin.database;
58+
expect(rtdbNamespace).to.not.be.null;
59+
const firebaseRtdb = require.cache[require.resolve('@firebase/database')];
60+
expect(firebaseRtdb).to.not.be.undefined;
61+
});
4862

49-
it('loads Firestore when calling admin.firestore', () => {
50-
const firestoreNamespace = admin.firestore;
51-
expect(firestoreNamespace).to.not.be.null;
52-
const gcloud = require.cache[require.resolve('@google-cloud/firestore')];
53-
expect(gcloud).to.not.be.undefined;
63+
it('does not load Firestore by default', () => {
64+
const gcloud = require.cache[require.resolve('@google-cloud/firestore')];
65+
expect(gcloud).to.be.undefined;
66+
});
67+
68+
it('loads Firestore when calling admin.firestore', () => {
69+
const firestoreNamespace = admin.firestore;
70+
expect(firestoreNamespace).to.not.be.null;
71+
const gcloud = require.cache[require.resolve('@google-cloud/firestore')];
72+
expect(gcloud).to.not.be.undefined;
73+
});
74+
75+
after(() => {
76+
dependencies.forEach((name) => {
77+
const resolvedName = require.resolve(name);
78+
require.cache[resolvedName] = tempCache[name];
79+
});
80+
return deleteApp(lazyLoadingApp);
81+
})
5482
});
5583
});
5684

@@ -98,3 +126,42 @@ describe('admin.app', () => {
98126
expect(admin.storage(app).app).to.deep.equal(app);
99127
});
100128
});
129+
130+
describe('getApp', () => {
131+
it('getApp() returns the default App', () => {
132+
const app = getApp();
133+
expect(app).to.deep.equal(defaultApp);
134+
expect(app.name).to.equal('[DEFAULT]');
135+
expect(app.options.databaseURL).to.equal(databaseUrl);
136+
expect(app.options.databaseAuthVariableOverride).to.be.undefined;
137+
expect(app.options.storageBucket).to.equal(storageBucket);
138+
});
139+
140+
it('getApp("null") returns the App named "null"', () => {
141+
const app = getApp('null');
142+
expect(app).to.deep.equal(nullApp);
143+
expect(app.name).to.equal('null');
144+
expect(app.options.databaseURL).to.equal(databaseUrl);
145+
expect(app.options.databaseAuthVariableOverride).to.be.null;
146+
expect(app.options.storageBucket).to.equal(storageBucket);
147+
});
148+
149+
it('getApp("nonNull") returns the App named "nonNull"', () => {
150+
const app = getApp('nonNull');
151+
expect(app).to.deep.equal(nonNullApp);
152+
expect(app.name).to.equal('nonNull');
153+
expect(app.options.databaseURL).to.equal(databaseUrl);
154+
expect((app.options.databaseAuthVariableOverride as any).uid).to.be.ok;
155+
expect(app.options.storageBucket).to.equal(storageBucket);
156+
});
157+
158+
it('namespace services are attached to the default App', () => {
159+
const app = getApp();
160+
expect(getAuth(app).app).to.deep.equal(app);
161+
});
162+
163+
it('namespace services are attached to the named App', () => {
164+
const app = getApp('null');
165+
expect(getAuth(app).app).to.deep.equal(app);
166+
});
167+
});

0 commit comments

Comments
 (0)