Skip to content

Commit ce0dc47

Browse files
committed
Make tests work again
1 parent 95cedd3 commit ce0dc47

File tree

14 files changed

+71
-50
lines changed

14 files changed

+71
-50
lines changed

integration/browserify/src/namespace.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ firebase.initializeApp({
2525
databaseURL: 'https://test-project-name.firebaseio.com',
2626
projectId: 'test-project-name',
2727
storageBucket: 'test-project-name.appspot.com',
28-
messagingSenderId: '012345678910'
28+
messagingSenderId: '012345678910',
29+
appId: 'myAppId'
2930
});
3031

3132
describe('Firebase Namespace Validation', function() {

integration/shared/namespaceDefinition.json

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
},
4141
"INTERNAL": {
4242
"__type": "object",
43-
"registerService": {
43+
"registerComponent": {
4444
"__type": "function"
4545
},
4646
"extendNamespace": {
@@ -55,20 +55,8 @@
5555
"removeApp": {
5656
"__type": "function"
5757
},
58-
"factories": {
59-
"__type": "object",
60-
"storage": {
61-
"__type": "function"
62-
},
63-
"auth": {
64-
"__type": "function"
65-
},
66-
"database": {
67-
"__type": "function"
68-
},
69-
"messaging": {
70-
"__type": "function"
71-
}
58+
"components": {
59+
"__type": "Map"
7260
},
7361
"ErrorFactory": {
7462
"__type": "function"

integration/typescript/test/namespace.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ firebase.initializeApp({
2525
databaseURL: 'https://test-project-name.firebaseio.com',
2626
projectId: 'test-project-name',
2727
storageBucket: 'test-project-name.appspot.com',
28-
messagingSenderId: '012345678910'
28+
messagingSenderId: '012345678910',
29+
appId: 'myAppId'
2930
});
3031

3132
describe('Firebase Namespace Validation', function() {

integration/webpack/src/namespace.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ firebase.initializeApp({
2525
databaseURL: 'https://test-project-name.firebaseio.com',
2626
projectId: 'test-project-name',
2727
storageBucket: 'test-project-name.appspot.com',
28-
messagingSenderId: '012345678910'
28+
messagingSenderId: '012345678910',
29+
appId: 'myAppId'
2930
});
3031

3132
describe('Firebase Namespace Validation', function() {

packages/app-types/index.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Provider } from '@firebase/component';
2-
31
/**
42
* @license
53
* Copyright 2017 Google Inc.
@@ -103,3 +101,9 @@ export interface FirebaseNamespace {
103101
// The current SDK version.
104102
SDK_VERSION: string;
105103
}
104+
105+
declare module '@firebase/component' {
106+
interface ComponentContainer {
107+
getProvider(name: 'app'): Provider<FirebaseApp>;
108+
}
109+
}

packages/app-types/private.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { FirebaseApp, FirebaseNamespace } from '@firebase/app-types';
2424
import { Observer, Subscribe } from '@firebase/util';
2525
import { FirebaseError, ErrorFactory } from '@firebase/util';
2626
import { Deferred } from '../firestore/test/util/promise';
27-
import { Component } from '@firebase/component';
27+
import { Component, ComponentContainer } from '@firebase/component';
2828

2929
export interface FirebaseServiceInternals {
3030
/**
@@ -82,7 +82,8 @@ export interface FirebaseAppInternals {
8282
}
8383

8484
export interface _FirebaseApp extends FirebaseApp {
85-
_addComponent(component: Component): void;
85+
container: ComponentContainer;
86+
_addComponent(component: Component, overwrite?: boolean): void;
8687
_removeServiceInstance(name: string, instanceIdentifier?: string): void;
8788
}
8889
export interface _FirebaseNamespace extends FirebaseNamespace {

packages/app/index.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseNamespace, FirebaseApp } from '@firebase/app-types';
18+
import { FirebaseNamespace } from '@firebase/app-types';
1919
import { firebase as firebaseNamespace } from './src/firebaseNamespace';
2020
import { isNode, isBrowser } from '@firebase/util';
21-
import { Provider } from '@firebase/component';
2221
import { logger } from './src/logger';
2322

2423
// Firebase Lite detection
@@ -70,9 +69,3 @@ export const firebase = firebaseNamespace;
7069

7170
// eslint-disable-next-line import/no-default-export
7271
export default firebase;
73-
74-
declare module '@firebase/component' {
75-
interface ComponentContainer {
76-
getProvider(name: 'app'): Provider<FirebaseApp>;
77-
}
78-
}

packages/app/src/firebaseApp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class FirebaseAppImpl implements FirebaseApp {
125125
// getImmediate will always succeed because _getService is only called for registered components.
126126
return this.container
127127
.getProvider(name)
128-
.getImmediate(instanceIdentifier) as FirebaseService;
128+
.getImmediate({identifier: instanceIdentifier}) as FirebaseService;
129129
}
130130
/**
131131
* Remove a service instance from the cache, so we will create a new instance for this service

packages/app/src/lite/firebaseAppLite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class FirebaseAppLiteImpl implements FirebaseApp {
135135
// getImmediate will always succeed because _getService is only called for registered components.
136136
return this.container
137137
.getProvider(name)
138-
.getImmediate(instanceIdentifier) as FirebaseService;
138+
.getImmediate({identifier: instanceIdentifier}) as FirebaseService;
139139
}
140140

141141
/**

packages/component/src/provider.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class Provider<T = unknown> {
3232
constructor(
3333
private readonly name: string,
3434
private readonly container: ComponentContainer
35-
) {}
35+
) { }
3636

3737
/**
3838
* @param identifier A provider can provide mulitple instances of a service
@@ -46,9 +46,14 @@ export class Provider<T = unknown> {
4646
const deferred = new Deferred<T>();
4747
this.instancesDeferred.set(normalizedIdentifier, deferred);
4848
// If the service instance is available, resolve the promise with it immediately
49-
const instance = this.getOrInitializeService(normalizedIdentifier);
50-
if (instance) {
51-
deferred.resolve(instance);
49+
try {
50+
const instance = this.getOrInitializeService(normalizedIdentifier);
51+
if (instance) {
52+
deferred.resolve(instance);
53+
}
54+
} catch (e) {
55+
// when the instance factory throws an exception during get(), it should not cause
56+
// an fatal error. We just return the unresolved promise in this case.
5257
}
5358
}
5459

@@ -76,17 +81,23 @@ export class Provider<T = unknown> {
7681
};
7782
// if multipleInstances is not supported, use the default name
7883
const normalizedIdentifier = this.normalizeInstanceIdentifier(identifier);
84+
try {
85+
const instance = this.getOrInitializeService(normalizedIdentifier);
7986

80-
const instance = this.getOrInitializeService(normalizedIdentifier);
81-
82-
if (!instance) {
83-
if (optional) {
87+
if (!instance) {
88+
if (optional) {
89+
return null;
90+
}
91+
throw Error(`Service ${this.name} is not available`);
92+
}
93+
return instance;
94+
} catch (e) {
95+
if(optional) {
8496
return null;
97+
} else {
98+
throw e;
8599
}
86-
throw Error(`Service ${this.name} is not available`);
87100
}
88-
89-
return instance;
90101
}
91102

92103
setComponent(component: Component<T>): void {
@@ -103,7 +114,14 @@ export class Provider<T = unknown> {
103114
this.component = component;
104115
// if the service is eager, initialize the default instance
105116
if (isComponentEager(component)) {
106-
this.getOrInitializeService(DEFAULT_ENTRY_NAME);
117+
try {
118+
this.getOrInitializeService(DEFAULT_ENTRY_NAME);
119+
} catch (e) {
120+
// when the instance factory for an eager Component throws an exception during the eager
121+
// initialization, it should not cause an fatal error.
122+
// TODO: Investigate if we need to make it configurable, because some component may want to cause
123+
// a fatal error in this case?
124+
}
107125
}
108126

109127
// Create service instances for the pending promises and resolve them
@@ -117,10 +135,15 @@ export class Provider<T = unknown> {
117135
instanceIdentifier
118136
);
119137

120-
// `getOrInitializeService()` should always return a valid instance since a component is guaranteed. use ! to make typescript happy.
121-
const instance = this.getOrInitializeService(normalizedIdentifier)!;
138+
try {
139+
// `getOrInitializeService()` should always return a valid instance since a component is guaranteed. use ! to make typescript happy.
140+
const instance = this.getOrInitializeService(normalizedIdentifier)!;
141+
instanceDeferred.resolve(instance);
142+
} catch (e) {
143+
// when the instance factory throws an exception, it should not cause
144+
// an fatal error. We just leave the promise unresolved.
145+
}
122146

123-
instanceDeferred.resolve(instance);
124147
}
125148
}
126149

packages/functions/src/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export class ContextProvider {
3737
authProvider: Provider<FirebaseAuthInternal>,
3838
messagingProvider: Provider<FirebaseMessaging>
3939
) {
40-
this.auth = authProvider.getImmediate(undefined, { optional: true });
41-
this.messaging = messagingProvider.getImmediate(undefined, {
40+
this.auth = authProvider.getImmediate({ optional: true });
41+
this.messaging = messagingProvider.getImmediate({
4242
optional: true
4343
});
4444

packages/messaging-types/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ export class FirebaseMessaging {
4949
useServiceWorker(registration: ServiceWorkerRegistration): void;
5050
usePublicVapidKey(b64PublicKey: string): void;
5151
}
52+
53+
declare module '@firebase/component' {
54+
interface ComponentContainer {
55+
getProvider(name: 'messaging'): Provider<FirebaseMessaging>;
56+
}
57+
58+
interface Provider { }
59+
}

packages/rxfire/test/database.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ describe('RxFire Database', () => {
9292
*/
9393
beforeEach(() => {
9494
app = initializeApp({
95+
apiKey: TEST_PROJECT.apiKey,
9596
projectId: TEST_PROJECT.projectId,
9697
databaseURL: TEST_PROJECT.databaseURL
9798
});

packages/storage/src/implementation/authwrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class AuthWrapper {
8989
}
9090

9191
getAuthToken(): Promise<string | null> {
92-
const auth = this.authProvider_.getImmediate(undefined, { optional: true });
92+
const auth = this.authProvider_.getImmediate({ optional: true });
9393
if (auth) {
9494
return auth.getToken().then(
9595
(response: FirebaseAuthTokenData | null): string | null => {

0 commit comments

Comments
 (0)