Skip to content

Commit c72e000

Browse files
committed
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 acf27af commit c72e000

File tree

15 files changed

+31
-45
lines changed

15 files changed

+31
-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
@@ -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: any;
102+
private _windowFocusTimeoutId: number;
103103

104104
/** The timeout id of the origin clearing timeout. */
105-
private _originTimeoutId: any;
105+
private _originTimeoutId: number;
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: any;
34+
private _previousTimeout: number;
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: any;
428+
let timeoutId: number;
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: 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: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +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 {
34-
return (typeof testGlobals.__karma__ !== 'undefined' && !!testGlobals.__karma__) ||
35-
(typeof testGlobals.jasmine !== 'undefined' && !!testGlobals.jasmine) ||
36-
(typeof testGlobals.jest !== 'undefined' && !!testGlobals.jest) ||
37-
(typeof testGlobals.Mocha !== 'undefined' && !!testGlobals.Mocha);
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)
15+
return (
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)
24+
);
3825
}

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", "node"],
15+
"types": ["jasmine"],
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: any;
106+
private _focusoutTimeout: number | null;
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
@@ -57,7 +57,7 @@ export class MatDialogContainer extends _MatDialogContainerBase implements OnDes
5757
private _closeAnimationDuration =
5858
this._animationsEnabled ? numbers.DIALOG_ANIMATION_CLOSE_TIME_MS : 0;
5959
/** Current timer for dialog animations. */
60-
private _animationTimer: any = null;
60+
private _animationTimer: number | null = null;
6161

6262
constructor(
6363
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: any;
72+
private _announceTimeoutId: number;
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: any;
42+
private _closeFallbackTimeout: number;
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: any;
49+
private _closeFallbackTimeout: number;
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: any;
78+
private _announceTimeoutId: number;
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: any;
49+
private _durationTimeoutId: number;
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: any;
251+
private _touchstartTimeout: number;
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: any;
769+
_showTimeoutId: number | undefined;
770770

771771
/** The timeout ID of any current timer set to hide the tooltip */
772-
_hideTimeoutId: any;
772+
_hideTimeoutId: number | undefined;
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: 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)