Skip to content

Commit

Permalink
Fix getApp() error message for non-autoinit situations (#7263)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubox76 committed Apr 26, 2023
1 parent 4535ccc commit 466d367
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-ways-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/app': patch
---

Make the error more helpful when `getApp()` is called before `initializeApp()`.
13 changes: 12 additions & 1 deletion packages/app/src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,20 @@ describe('API tests', () => {
expect(getApp(appName)).to.equal(app);
});

it('throws retrieving a non existing App', () => {
it('throws retrieving a non existing App (custom name)', () => {
expect(() => getApp('RandomName')).throws(/No Firebase App 'RandomName'/);
});

it('throws retrieving a non existing App (default name)', () => {
expect(() => getApp()).throws(/No Firebase App/);
});

it('does not throw on a non existing App (default name) if a defaults object exists', () => {
global.__FIREBASE_DEFAULTS__ = { config: { apiKey: 'abcd' } };
const app = getApp();
expect(app.options.apiKey).to.equal('abcd');
global.__FIREBASE_DEFAULTS__ = undefined;
});
});

describe('getApps', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export function initializeApp(
*/
export function getApp(name: string = DEFAULT_ENTRY_NAME): FirebaseApp {
const app = _apps.get(name);
if (!app && name === DEFAULT_ENTRY_NAME) {
if (!app && name === DEFAULT_ENTRY_NAME && getDefaultAppConfig()) {
return initializeApp();
}
if (!app) {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const enum AppError {
const ERRORS: ErrorMap<AppError> = {
[AppError.NO_APP]:
"No Firebase App '{$appName}' has been created - " +
'call Firebase App.initializeApp()',
'call initializeApp() first',
[AppError.BAD_APP_NAME]: "Illegal App name: '{$appName}",
[AppError.DUPLICATE_APP]:
"Firebase App named '{$appName}' already exists with different options or config",
Expand Down
3 changes: 3 additions & 0 deletions packages/app/src/internal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
_getProvider,
_removeServiceInstance
} from './internal';
import { logger } from './logger';

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

_addComponent(app, testComp);

expect(() => _addComponent(app, testComp)).to.not.throw();
expect(debugStub).to.be.called;
expect(app.container.getProvider('test').getComponent()).to.equal(
testComp
);
Expand Down

0 comments on commit 466d367

Please sign in to comment.