Skip to content

Commit 81e467d

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 81e467d

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

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 {

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"strictBindCallApply": true,
2222
"target": "es2015",
2323
"lib": ["es5", "es2015", "dom"],
24-
"types": ["jasmine"],
24+
"types": ["jasmine", "node"],
2525
"baseUrl": ".",
2626
"paths": {
2727
"@angular/cdk": ["./src/cdk"],

0 commit comments

Comments
 (0)