Skip to content

Fix getApp() error message for non-autoinit situations #7263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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