Skip to content

Commit 466d367

Browse files
authored
Fix getApp() error message for non-autoinit situations (#7263)
1 parent 4535ccc commit 466d367

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

.changeset/funny-ways-carry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/app': patch
3+
---
4+
5+
Make the error more helpful when `getApp()` is called before `initializeApp()`.

packages/app/src/api.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,20 @@ describe('API tests', () => {
199199
expect(getApp(appName)).to.equal(app);
200200
});
201201

202-
it('throws retrieving a non existing App', () => {
202+
it('throws retrieving a non existing App (custom name)', () => {
203203
expect(() => getApp('RandomName')).throws(/No Firebase App 'RandomName'/);
204204
});
205+
206+
it('throws retrieving a non existing App (default name)', () => {
207+
expect(() => getApp()).throws(/No Firebase App/);
208+
});
209+
210+
it('does not throw on a non existing App (default name) if a defaults object exists', () => {
211+
global.__FIREBASE_DEFAULTS__ = { config: { apiKey: 'abcd' } };
212+
const app = getApp();
213+
expect(app.options.apiKey).to.equal('abcd');
214+
global.__FIREBASE_DEFAULTS__ = undefined;
215+
});
205216
});
206217

207218
describe('getApps', () => {

packages/app/src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export function initializeApp(
202202
*/
203203
export function getApp(name: string = DEFAULT_ENTRY_NAME): FirebaseApp {
204204
const app = _apps.get(name);
205-
if (!app && name === DEFAULT_ENTRY_NAME) {
205+
if (!app && name === DEFAULT_ENTRY_NAME && getDefaultAppConfig()) {
206206
return initializeApp();
207207
}
208208
if (!app) {

packages/app/src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const enum AppError {
3434
const ERRORS: ErrorMap<AppError> = {
3535
[AppError.NO_APP]:
3636
"No Firebase App '{$appName}' has been created - " +
37-
'call Firebase App.initializeApp()',
37+
'call initializeApp() first',
3838
[AppError.BAD_APP_NAME]: "Illegal App name: '{$appName}",
3939
[AppError.DUPLICATE_APP]:
4040
"Firebase App named '{$appName}' already exists with different options or config",

packages/app/src/internal.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
_getProvider,
3131
_removeServiceInstance
3232
} from './internal';
33+
import { logger } from './logger';
3334

3435
declare module '@firebase/component' {
3536
interface NameServiceMapping {
@@ -60,10 +61,12 @@ describe('Internal API tests', () => {
6061
it('does NOT throw registering duplicate components', () => {
6162
const app = initializeApp({}) as FirebaseAppImpl;
6263
const testComp = createTestComponent('test');
64+
const debugStub = stub(logger, 'debug');
6365

6466
_addComponent(app, testComp);
6567

6668
expect(() => _addComponent(app, testComp)).to.not.throw();
69+
expect(debugStub).to.be.called;
6770
expect(app.container.getProvider('test').getComponent()).to.equal(
6871
testComp
6972
);

0 commit comments

Comments
 (0)