From 4624ca823d7000a4a9c039e1dab00746126d5d34 Mon Sep 17 00:00:00 2001 From: Jeremy Elbourn Date: Mon, 28 Jun 2021 11:30:32 -0700 Subject: [PATCH] fix(cdk/overlay): use interface for test environment globals (#23041) The previous approach of using `declare const` was causing collisions with the real test environment types inside Google. (cherry picked from commit da91c1828ad64476218d1dfe6866f2437aa2b2e4) --- src/cdk/overlay/overlay-container.ts | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/cdk/overlay/overlay-container.ts b/src/cdk/overlay/overlay-container.ts index f4ff864a48a3..fac5e1bba66b 100644 --- a/src/cdk/overlay/overlay-container.ts +++ b/src/cdk/overlay/overlay-container.ts @@ -10,20 +10,29 @@ import {DOCUMENT} from '@angular/common'; import {Inject, Injectable, OnDestroy} from '@angular/core'; import {Platform} from '@angular/cdk/platform'; -declare const __karma__: unknown; -declare const jasmine: unknown; -declare const jest: unknown; -declare const Mocha: unknown; +// Avoid using `declare const` because it caused conflicts inside Google +// with the real typings for these symbols. We use `declare interface` instead +// of just `interface` for interop with Closure Compiler (prevents property renaming): +// https://github.com/angular/tsickle/blob/master/README.md#differences-from-typescript +declare interface TestGlobals { + jasmine: unknown; + __karma__: unknown; + jest: unknown; + Mocha: unknown; +} + +const globalsForTest = (typeof window !== 'undefined' ? window : {}) as {} as TestGlobals; /** * Whether we're in a testing environment. * TODO(crisbeto): remove this once we have an overlay testing module or Angular starts tearing * down the testing `NgModule` (see https://github.com/angular/angular/issues/18831). */ -const isTestEnvironment = (typeof __karma__ !== 'undefined' && !!__karma__) || - (typeof jasmine !== 'undefined' && !!jasmine) || - (typeof jest !== 'undefined' && !!jest) || - (typeof Mocha !== 'undefined' && !!Mocha); +const isTestEnvironment = + (typeof globalsForTest.__karma__ !== 'undefined' && !!globalsForTest.__karma__) || + (typeof globalsForTest.jasmine !== 'undefined' && !!globalsForTest.jasmine) || + (typeof globalsForTest.jest !== 'undefined' && !!globalsForTest.jest) || + (typeof globalsForTest.Mocha !== 'undefined' && !!globalsForTest.Mocha); /** Container inside which all overlays will render. */ @Injectable({providedIn: 'root'})