Skip to content

Commit 2cb53ec

Browse files
committed
fix(cdk/overlay): use interface for test environment globals
The previous approach of using `declare const` was causing collisions with the real test environment types inside Google.
1 parent 4caf039 commit 2cb53ec

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/cdk/overlay/overlay-container.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,29 @@ import {DOCUMENT} from '@angular/common';
1010
import {Inject, Injectable, OnDestroy} from '@angular/core';
1111
import {Platform} from '@angular/cdk/platform';
1212

13-
declare const __karma__: unknown;
14-
declare const jasmine: unknown;
15-
declare const jest: unknown;
16-
declare const Mocha: unknown;
13+
// Avoid using `declare const` because it caused conflicts inside Google
14+
// with the real typings for these symbols. We use `declare interface` instead
15+
// of just `interface` for interop with Closure Compiler (prevents property renaming):
16+
// https://github.com/angular/tsickle/blob/master/README.md#differences-from-typescript
17+
declare interface TestGlobals {
18+
jasmine: unknown;
19+
__karma__: unknown;
20+
jest: unknown;
21+
Mocha: unknown;
22+
}
23+
24+
const globalsForTest = (typeof window !== 'undefined' ? window : {}) as {} as TestGlobals;
1725

1826
/**
1927
* Whether we're in a testing environment.
2028
* TODO(crisbeto): remove this once we have an overlay testing module or Angular starts tearing
2129
* down the testing `NgModule` (see https://github.com/angular/angular/issues/18831).
2230
*/
23-
const isTestEnvironment = (typeof __karma__ !== 'undefined' && !!__karma__) ||
24-
(typeof jasmine !== 'undefined' && !!jasmine) ||
25-
(typeof jest !== 'undefined' && !!jest) ||
26-
(typeof Mocha !== 'undefined' && !!Mocha);
31+
const isTestEnvironment =
32+
(typeof globalsForTest.__karma__ !== 'undefined' && !!globalsForTest.__karma__) ||
33+
(typeof globalsForTest.jasmine !== 'undefined' && !!globalsForTest.jasmine) ||
34+
(typeof globalsForTest.jest !== 'undefined' && !!globalsForTest.jest) ||
35+
(typeof globalsForTest.Mocha !== 'undefined' && !!globalsForTest.Mocha);
2736

2837
/** Container inside which all overlays will render. */
2938
@Injectable({providedIn: 'root'})

0 commit comments

Comments
 (0)