diff --git a/packages/analytics/index.test.ts b/packages/analytics/index.test.ts index 1dc31949c80..8b51306d86e 100644 --- a/packages/analytics/index.test.ts +++ b/packages/analytics/index.test.ts @@ -93,7 +93,12 @@ describe('FirebaseAnalytics instance tests', () => { ); }); it('Warns if config has no apiKey but does have a measurementId', () => { + // Since this is a warning and doesn't block the rest of initialization + // all the async stuff needs to be stubbed and cleaned up. const warnStub = stub(console, 'warn'); + const docStub = stub(document, 'createElement'); + stubFetch(200, { measurementId: fakeMeasurementId }); + stubIdbOpen(); const app = getFakeApp({ appId: fakeAppParams.appId, measurementId: fakeMeasurementId @@ -104,6 +109,11 @@ describe('FirebaseAnalytics instance tests', () => { `Falling back to the measurement ID ${fakeMeasurementId}` ); warnStub.restore(); + docStub.restore(); + fetchStub.restore(); + idbOpenStub.restore(); + delete window['gtag']; + delete window['dataLayer']; }); it('Throws if creating an instance with already-used appId', () => { const app = getFakeApp(fakeAppParams); @@ -210,6 +220,7 @@ describe('FirebaseAnalytics instance tests', () => { afterEach(() => { delete window['gtag']; delete window['dataLayer']; + removeGtagScript(); fetchStub.restore(); clock.restore(); warnStub.restore(); diff --git a/packages/analytics/src/factory.ts b/packages/analytics/src/factory.ts index 3bd742304c0..d6a7359aaa0 100644 --- a/packages/analytics/src/factory.ts +++ b/packages/analytics/src/factory.ts @@ -29,12 +29,7 @@ import { setUserProperties, setAnalyticsCollectionEnabled } from './functions'; -import { - insertScriptTag, - getOrCreateDataLayer, - wrapOrCreateGtag, - findGtagScriptOnPage -} from './helpers'; +import { getOrCreateDataLayer, wrapOrCreateGtag } from './helpers'; import { AnalyticsError, ERROR_FACTORY } from './errors'; import { FirebaseApp } from '@firebase/app-types'; import { FirebaseInstallations } from '@firebase/installations-types'; @@ -61,9 +56,9 @@ let initializationPromisesMap: { * wait on all these to be complete in order to determine if it can selectively * wait for only certain initialization (FID) promises or if it must wait for all. */ -let dynamicConfigPromisesList: Array> = []; +let dynamicConfigPromisesList: Array< + Promise +> = []; /** * Maps fetched measurementIds to appId. Populated when the app's dynamic config @@ -202,10 +197,6 @@ export function factory( // Steps here should only be done once per page: creation or wrapping // of dataLayer and global gtag function. - // Detect if user has already put the gtag