Skip to content

Commit 4f6b9fd

Browse files
authored
fix(material/core): sanity checks not disabled for node-based test environments (#23636)
In #23374 we expanded the logic that checks for test environments to cover Jest and Mocha. The problem is that we were checking against the `window` which won't work in a Node environment, because the global variables are attached to the `global` object, not the `window`, even though there may be a fake `window` declared by the test tooling. These changes resolve the issue by first checking against `global` before falling back to `window`. Fixes #23365.
1 parent 3181780 commit 4f6b9fd

File tree

15 files changed

+29
-18
lines changed

15 files changed

+29
-18
lines changed

src/cdk/a11y/focus-monitor/focus-monitor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ export class FocusMonitor implements OnDestroy {
9999
private _windowFocused = false;
100100

101101
/** The timeout id of the window focus timeout. */
102-
private _windowFocusTimeoutId: number;
102+
private _windowFocusTimeoutId: any;
103103

104104
/** The timeout id of the origin clearing timeout. */
105-
private _originTimeoutId: number;
105+
private _originTimeoutId: any;
106106

107107
/**
108108
* Whether the origin was determined via a touch interaction. Necessary as properly attributing

src/cdk/a11y/live-announcer/live-announcer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
export class LiveAnnouncer implements OnDestroy {
3232
private _liveElement: HTMLElement;
3333
private _document: Document;
34-
private _previousTimeout?: number;
34+
private _previousTimeout: any;
3535

3636
constructor(
3737
@Optional() @Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN) elementToken: any,

src/cdk/overlay/overlay-ref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ export class OverlayRef implements PortalOutlet, OverlayReference {
425425
return;
426426
}
427427

428-
let timeoutId: number;
428+
let timeoutId: any;
429429
const finishDetach = () => {
430430
// It may not be attached to anything in certain cases (e.g. unit tests).
431431
if (backdropToDetach) {

src/cdk/platform/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ng_module(
1111
deps = [
1212
"@npm//@angular/common",
1313
"@npm//@angular/core",
14+
"@npm//@types/node",
1415
],
1516
)
1617

src/cdk/platform/features/test-environment.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@ declare interface TestGlobals {
1717
Mocha: unknown;
1818
}
1919

20-
const testGlobals = (typeof window !== 'undefined' ? window : {}) as {} as TestGlobals;
20+
let testGlobals: TestGlobals;
21+
22+
// We check the Node-specific `global` first, because tools tend to add a fake
23+
// `window` in Node environments which won't actually receive global variables.
24+
if (typeof global !== 'undefined') {
25+
testGlobals = global as {} as TestGlobals;
26+
} else if (typeof window !== 'undefined') {
27+
testGlobals = window as {} as TestGlobals;
28+
} else {
29+
testGlobals = {} as TestGlobals;
30+
}
2131

2232
/** Gets whether the code is currently running in a test environment. */
2333
export function _isTestEnvironment(): boolean {

src/cdk/tsconfig-tests.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"importHelpers": false,
1313
"module": "umd",
1414
"target": "es5",
15-
"types": ["jasmine"],
15+
"types": ["jasmine", "node"],
1616
"experimentalDecorators": true,
1717
"emitDecoratorMetadata": true,
1818
"paths": {

src/material-experimental/mdc-chips/chip-row.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class MatChipRow extends MatChip implements AfterContentInit, AfterViewIn
103103
* Timeout used to give some time between `focusin` and `focusout`
104104
* in order to determine whether focus has left the chip.
105105
*/
106-
private _focusoutTimeout: number | null;
106+
private _focusoutTimeout: any;
107107

108108
constructor(
109109
@Inject(DOCUMENT) private readonly _document: any,

src/material-experimental/mdc-dialog/dialog-container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class MatDialogContainer extends _MatDialogContainerBase implements OnDes
5858
private _closeAnimationDuration =
5959
this._animationsEnabled ? numbers.DIALOG_ANIMATION_CLOSE_TIME_MS : 0;
6060
/** Current timer for dialog animations. */
61-
private _animationTimer: number|null = null;
61+
private _animationTimer: any = null;
6262

6363
constructor(
6464
elementRef: ElementRef,

src/material-experimental/mdc-snack-bar/snack-bar-container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export class MatSnackBarContainer extends BasePortalOutlet
6969
private readonly _announceDelay: number = 150;
7070

7171
/** The timeout for announcing the snack bar's content. */
72-
private _announceTimeoutId: number;
72+
private _announceTimeoutId: any;
7373

7474
/** Subject for notifying that the snack bar has announced to screen readers. */
7575
readonly _onAnnounce: Subject<void> = new Subject();

src/material/bottom-sheet/bottom-sheet-ref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class MatBottomSheetRef<T = any, R = any> {
3939
private _result: R | undefined;
4040

4141
/** Handle to the timeout that's running as a fallback in case the exit animation doesn't fire. */
42-
private _closeFallbackTimeout: number;
42+
private _closeFallbackTimeout: any;
4343

4444
constructor(
4545
containerInstance: MatBottomSheetContainer,

src/material/dialog/dialog-ref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class MatDialogRef<T, R = any> {
4646
private _result: R | undefined;
4747

4848
/** Handle to the timeout that's running as a fallback in case the exit animation doesn't fire. */
49-
private _closeFallbackTimeout: number;
49+
private _closeFallbackTimeout: any;
5050

5151
/** Current state of the dialog. */
5252
private _state = MatDialogState.OPEN;

src/material/snack-bar/snack-bar-container.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class MatSnackBarContainer extends BasePortalOutlet
7575
private readonly _announceDelay: number = 150;
7676

7777
/** The timeout for announcing the snack bar's content. */
78-
private _announceTimeoutId: number;
78+
private _announceTimeoutId: any;
7979

8080
/** Whether the component has been destroyed. */
8181
private _destroyed = false;

src/material/snack-bar/snack-bar-ref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class MatSnackBarRef<T> {
4646
* Timeout ID for the duration setTimeout call. Used to clear the timeout if the snackbar is
4747
* dismissed before the duration passes.
4848
*/
49-
private _durationTimeoutId: number;
49+
private _durationTimeoutId: any;
5050

5151
/** Whether the snack bar was dismissed using the action button. */
5252
private _dismissedByAction = false;

src/material/tooltip/tooltip.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export abstract class _MatTooltipBase<T extends _TooltipComponentBase> implement
248248
private _document: Document;
249249

250250
/** Timer started at the last `touchstart` event. */
251-
private _touchstartTimeout: number;
251+
private _touchstartTimeout: any;
252252

253253
/** Emits when the component is destroyed. */
254254
private readonly _destroyed = new Subject<void>();
@@ -766,10 +766,10 @@ export abstract class _TooltipComponentBase implements OnDestroy {
766766
tooltipClass: string|string[]|Set<string>|{[key: string]: any};
767767

768768
/** The timeout ID of any current timer set to show the tooltip */
769-
_showTimeoutId: number | undefined;
769+
_showTimeoutId: any;
770770

771771
/** The timeout ID of any current timer set to hide the tooltip */
772-
_hideTimeoutId: number | undefined;
772+
_hideTimeoutId: any;
773773

774774
/** Property watched by the animation framework to show or hide the tooltip */
775775
_visibility: TooltipVisibility = 'initial';

tools/public_api_guard/material/tooltip.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ export abstract class _TooltipComponentBase implements OnDestroy {
183183
_animationStart(): void;
184184
_handleBodyInteraction(): void;
185185
hide(delay: number): void;
186-
_hideTimeoutId: number | undefined;
186+
_hideTimeoutId: any;
187187
isVisible(): boolean;
188188
_markForCheck(): void;
189189
message: string;
190190
// (undocumented)
191191
ngOnDestroy(): void;
192192
protected _onShow(): void;
193193
show(delay: number): void;
194-
_showTimeoutId: number | undefined;
194+
_showTimeoutId: any;
195195
tooltipClass: string | string[] | Set<string> | {
196196
[key: string]: any;
197197
};

0 commit comments

Comments
 (0)