Skip to content

Commit be6774f

Browse files
committed
fix(material/core): sanity checks not disabled for node-based test environments
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 f466a2b commit be6774f

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
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/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": {

0 commit comments

Comments
 (0)