Skip to content

Commit ff7fd48

Browse files
authored
fix(material/core): test environment check not picking up jest (#23722)
In #23636 the test environment check was changed so that it looks for the test objects either on `window` or `global`, however it looks like this isn't enough to pick up Jest which isn't published on either. These changes simplify the setup by looking up the value globally and disabling the type checkng error with `@ts-ignore`. We can't use `declare const` for it, because it causes issues in g3. I've also reverted some of the `any` types that had to be added in #23636. Fixes #23365.
1 parent be9abca commit ff7fd48

File tree

15 files changed

+29
-45
lines changed

15 files changed

+29
-45
lines changed

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

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

9797
/** The timeout id of the window focus timeout. */
98-
private _windowFocusTimeoutId: any;
98+
private _windowFocusTimeoutId: number;
9999

100100
/** The timeout id of the origin clearing timeout. */
101-
private _originTimeoutId: any;
101+
private _originTimeoutId: number;
102102

103103
/**
104104
* 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
@@ -30,7 +30,7 @@ import {
3030
export class LiveAnnouncer implements OnDestroy {
3131
private _liveElement: HTMLElement;
3232
private _document: Document;
33-
private _previousTimeout: any;
33+
private _previousTimeout: number;
3434

3535
constructor(
3636
@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
@@ -418,7 +418,7 @@ export class OverlayRef implements PortalOutlet, OverlayReference {
418418
return;
419419
}
420420

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

src/cdk/platform/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ ng_module(
1111
deps = [
1212
"@npm//@angular/common",
1313
"@npm//@angular/core",
14-
"@npm//@types/node",
1514
],
1615
)
1716

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

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,20 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

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-
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-
}
31-
329
/** Gets whether the code is currently running in a test environment. */
3310
export function _isTestEnvironment(): boolean {
11+
// We can't use `declare const` because it causes conflicts inside Google with the real typings
12+
// for these symbols and we can't read them off the global object, because they don't appear to
13+
// be attached there for some runners like Jest.
14+
// (see: https://github.com/angular/components/issues/23365#issuecomment-938146643)
3415
return (
35-
(typeof testGlobals.__karma__ !== 'undefined' && !!testGlobals.__karma__) ||
36-
(typeof testGlobals.jasmine !== 'undefined' && !!testGlobals.jasmine) ||
37-
(typeof testGlobals.jest !== 'undefined' && !!testGlobals.jest) ||
38-
(typeof testGlobals.Mocha !== 'undefined' && !!testGlobals.Mocha)
16+
// @ts-ignore
17+
(typeof __karma__ !== 'undefined' && !!__karma__) ||
18+
// @ts-ignore
19+
(typeof jasmine !== 'undefined' && !!jasmine) ||
20+
// @ts-ignore
21+
(typeof jest !== 'undefined' && !!jest) ||
22+
// @ts-ignore
23+
(typeof Mocha !== 'undefined' && !!Mocha)
3924
);
4025
}

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

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

109109
constructor(
110110
@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
@@ -60,7 +60,7 @@ export class MatDialogContainer extends _MatDialogContainerBase implements OnDes
6060
? numbers.DIALOG_ANIMATION_CLOSE_TIME_MS
6161
: 0;
6262
/** Current timer for dialog animations. */
63-
private _animationTimer: any = null;
63+
private _animationTimer: number | null = null;
6464

6565
constructor(
6666
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
@@ -71,7 +71,7 @@ export class MatSnackBarContainer
7171
private readonly _announceDelay: number = 150;
7272

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

7676
/** Subject for notifying that the snack bar has announced to screen readers. */
7777
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
@@ -38,7 +38,7 @@ export class MatBottomSheetRef<T = any, R = any> {
3838
private _result: R | undefined;
3939

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

4343
constructor(containerInstance: MatBottomSheetContainer, private _overlayRef: OverlayRef) {
4444
this.containerInstance = containerInstance;

src/material/dialog/dialog-ref.ts

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

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

5454
/** Current state of the dialog. */
5555
private _state = MatDialogState.OPEN;

0 commit comments

Comments
 (0)