Skip to content

Commit 01d8e89

Browse files
crisbetowagnermaciel
authored andcommitted
fix(material/core): avoid running sanity checks on some test environments (#23374)
Fixes that the sanity checks were running on some testing environments like Jest. These changes also move the test environment check into a centralized place so that it's easier to keep up to date. Fixes #23365. (cherry picked from commit ca67623)
1 parent dc658be commit 01d8e89

File tree

5 files changed

+41
-41
lines changed

5 files changed

+41
-41
lines changed

src/cdk/overlay/overlay-container.ts

+7-28
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,7 @@
88

99
import {DOCUMENT} from '@angular/common';
1010
import {Inject, Injectable, OnDestroy} from '@angular/core';
11-
import {Platform} from '@angular/cdk/platform';
12-
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;
25-
26-
/**
27-
* Whether we're in a testing environment.
28-
* TODO(crisbeto): remove this once we have an overlay testing module or Angular starts tearing
29-
* down the testing `NgModule` (see https://github.com/angular/angular/issues/18831).
30-
*/
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);
11+
import {Platform, _isTestEnvironment} from '@angular/cdk/platform';
3612

3713
/** Container inside which all overlays will render. */
3814
@Injectable({providedIn: 'root'})
@@ -54,7 +30,7 @@ export class OverlayContainer implements OnDestroy {
5430

5531
/**
5632
* This method returns the overlay container element. It will lazily
57-
* create the element the first time it is called to facilitate using
33+
* create the element the first time it is called to facilitate using
5834
* the container in non-browser environments.
5935
* @returns the container element
6036
*/
@@ -73,7 +49,10 @@ export class OverlayContainer implements OnDestroy {
7349
protected _createContainer(): void {
7450
const containerClass = 'cdk-overlay-container';
7551

76-
if (this._platform.isBrowser || isTestEnvironment) {
52+
// TODO(crisbeto): remove the testing check once we have an overlay testing
53+
// module or Angular starts tearing down the testing `NgModule`. See:
54+
// https://github.com/angular/angular/issues/18831
55+
if (this._platform.isBrowser || _isTestEnvironment()) {
7756
const oppositePlatformContainers =
7857
this._document.querySelectorAll(`.${containerClass}[platform="server"], ` +
7958
`.${containerClass}[platform="test"]`);
@@ -97,7 +76,7 @@ export class OverlayContainer implements OnDestroy {
9776
// module which does the cleanup, we try to detect that we're in a test environment and we
9877
// always clear the container. See #17006.
9978
// TODO(crisbeto): remove the test environment check once we have an overlay testing module.
100-
if (isTestEnvironment) {
79+
if (_isTestEnvironment()) {
10180
container.setAttribute('platform', 'test');
10281
} else if (!this._platform.isBrowser) {
10382
container.setAttribute('platform', 'server');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
// Avoid using `declare const` because it caused conflicts inside Google
10+
// with the real typings for these symbols. We use `declare interface` instead
11+
// of just `interface` for interop with Closure Compiler (prevents property renaming):
12+
// https://github.com/angular/tsickle/blob/master/README.md#differences-from-typescript
13+
declare interface TestGlobals {
14+
jasmine: unknown;
15+
__karma__: unknown;
16+
jest: unknown;
17+
Mocha: unknown;
18+
}
19+
20+
const testGlobals = (typeof window !== 'undefined' ? window : {}) as {} as TestGlobals;
21+
22+
/** Gets whether the code is currently running in a test environment. */
23+
export function _isTestEnvironment(): boolean {
24+
return (typeof testGlobals.__karma__ !== 'undefined' && !!testGlobals.__karma__) ||
25+
(typeof testGlobals.jasmine !== 'undefined' && !!testGlobals.jasmine) ||
26+
(typeof testGlobals.jest !== 'undefined' && !!testGlobals.jest) ||
27+
(typeof testGlobals.Mocha !== 'undefined' && !!testGlobals.Mocha);
28+
}

src/cdk/platform/public-api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export * from './features/input-types';
1212
export * from './features/passive-listeners';
1313
export * from './features/scrolling';
1414
export * from './features/shadow-dom';
15+
export * from './features/test-environment';

src/material/core/common-behaviors/common-module.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {BidiModule} from '@angular/cdk/bidi';
1111
import {Inject, InjectionToken, isDevMode, NgModule, Optional, Version} from '@angular/core';
1212
import {VERSION as CDK_VERSION} from '@angular/cdk';
1313
import {DOCUMENT} from '@angular/common';
14+
import {_isTestEnvironment} from '@angular/cdk/platform';
1415

1516
// Private version constant to circumvent test/build issues,
1617
// i.e. avoid core to depend on the @angular/material primary entry-point
@@ -84,19 +85,13 @@ export class MatCommonModule {
8485
}
8586
}
8687

87-
/** Use defaultView of injected document if available or fallback to global window reference */
88-
private _getWindow(): Window | null {
89-
const win = this._document.defaultView || window;
90-
return typeof win === 'object' && win ? win : null;
91-
}
92-
9388
/** Gets whether a specific sanity check is enabled. */
9489
private _checkIsEnabled(name: keyof GranularSanityChecks): boolean {
9590
// TODO(crisbeto): we can't use `ngDevMode` here yet, because ViewEngine apps might not support
9691
// it. Since these checks can have performance implications and they aren't tree shakeable
9792
// in their current form, we can leave the `isDevMode` check in for now.
9893
// tslint:disable-next-line:ban
99-
if (!isDevMode() || this._isTestEnv()) {
94+
if (!isDevMode() || _isTestEnvironment()) {
10095
return false;
10196
}
10297

@@ -107,12 +102,6 @@ export class MatCommonModule {
107102
return !!this._sanityChecks[name];
108103
}
109104

110-
/** Whether the code is running in tests. */
111-
private _isTestEnv() {
112-
const window = this._getWindow() as any;
113-
return window && (window.__karma__ || window.jasmine);
114-
}
115-
116105
private _checkDoctypeIsDefined(): void {
117106
if (this._checkIsEnabled('doctype') && !this._document.doctype) {
118107
console.warn(

tools/public_api_guard/cdk/platform.md

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ export function _getShadowRoot(element: HTMLElement): ShadowRoot | null;
2121
// @public (undocumented)
2222
export function getSupportedInputTypes(): Set<string>;
2323

24+
// @public
25+
export function _isTestEnvironment(): boolean;
26+
2427
// @public
2528
export function normalizePassiveListenerOptions(options: AddEventListenerOptions): AddEventListenerOptions | boolean;
2629

0 commit comments

Comments
 (0)