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;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class MatSnackBarContainer
7777
private readonly _announceDelay: number = 150;
7878

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

8282
/** Whether the component has been destroyed. */
8383
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
@@ -45,7 +45,7 @@ export class MatSnackBarRef<T> {
4545
* Timeout ID for the duration setTimeout call. Used to clear the timeout if the snackbar is
4646
* dismissed before the duration passes.
4747
*/
48-
private _durationTimeoutId: any;
48+
private _durationTimeoutId: number;
4949

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

src/material/tooltip/tooltip.ts

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

260260
/** Timer started at the last `touchstart` event. */
261-
private _touchstartTimeout: any;
261+
private _touchstartTimeout: number;
262262

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

809809
/** The timeout ID of any current timer set to show the tooltip */
810-
_showTimeoutId: any;
810+
_showTimeoutId: number | undefined;
811811

812812
/** The timeout ID of any current timer set to hide the tooltip */
813-
_hideTimeoutId: any;
813+
_hideTimeoutId: number | undefined;
814814

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

src/tsconfig-legacy.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"paths": {
99
"@angular/*": ["./*"]
1010
},
11-
"types": ["jasmine", "node"],
11+
"types": ["jasmine"],
1212
"emitDecoratorMetadata": true,
1313
"experimentalDecorators": true
1414
},

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: any;
186+
_hideTimeoutId: number | undefined;
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: any;
194+
_showTimeoutId: number | undefined;
195195
tooltipClass: string | string[] | Set<string> | {
196196
[key: string]: any;
197197
};

0 commit comments

Comments
 (0)