Skip to content

fix(material/core): sanity checks not disabled for node-based test environments #23636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cdk/a11y/focus-monitor/focus-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ export class FocusMonitor implements OnDestroy {
private _windowFocused = false;

/** The timeout id of the window focus timeout. */
private _windowFocusTimeoutId: number;
private _windowFocusTimeoutId: any;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are necessary, because the type now is number | NodeJS.Timer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. looks like these any casts are just needed for the legacy Saucelabs setup. Should we instead just declare the global type directly instead? I think that would be better than giving up on type safety. e.g.

declare const global: any;

Copy link
Member Author

@crisbeto crisbeto Sep 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a comment at the top of that file about avoiding declare const due to an issue with Closure so that's why I wrote it this way in the first place. Otherwise the same could've been written as declare cost jest: any.

Copy link
Member

@devversion devversion Sep 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh that's unfortunate. What is about globalThis? this should work as well and not need any Node types. Seems like it's ideal for such platform-agnostic checks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I avoided it since it isn't supported in some browsers (not sure if we care about them) and the workaround wasn't that much more code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, I don't feel very strongly about it. It seems supported by all of the browsers we make guarantees for (https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/globalThis), and it would save us from using any in all of cdk/ (for the timers).


/** The timeout id of the origin clearing timeout. */
private _originTimeoutId: number;
private _originTimeoutId: any;

/**
* Whether the origin was determined via a touch interaction. Necessary as properly attributing
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/a11y/live-announcer/live-announcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
export class LiveAnnouncer implements OnDestroy {
private _liveElement: HTMLElement;
private _document: Document;
private _previousTimeout?: number;
private _previousTimeout: any;

constructor(
@Optional() @Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN) elementToken: any,
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/overlay/overlay-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ export class OverlayRef implements PortalOutlet, OverlayReference {
return;
}

let timeoutId: number;
let timeoutId: any;
const finishDetach = () => {
// It may not be attached to anything in certain cases (e.g. unit tests).
if (backdropToDetach) {
Expand Down
1 change: 1 addition & 0 deletions src/cdk/platform/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ng_module(
deps = [
"@npm//@angular/common",
"@npm//@angular/core",
"@npm//@types/node",
],
)

Expand Down
12 changes: 11 additions & 1 deletion src/cdk/platform/features/test-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ declare interface TestGlobals {
Mocha: unknown;
}

const testGlobals = (typeof window !== 'undefined' ? window : {}) as {} as TestGlobals;
let testGlobals: TestGlobals;

// We check the Node-specific `global` first, because tools tend to add a fake
// `window` in Node environments which won't actually receive global variables.
if (typeof global !== 'undefined') {
testGlobals = global as {} as TestGlobals;
} else if (typeof window !== 'undefined') {
testGlobals = window as {} as TestGlobals;
} else {
testGlobals = {} as TestGlobals;
}

/** Gets whether the code is currently running in a test environment. */
export function _isTestEnvironment(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/cdk/tsconfig-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"importHelpers": false,
"module": "umd",
"target": "es5",
"types": ["jasmine"],
"types": ["jasmine", "node"],
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"paths": {
Expand Down
2 changes: 1 addition & 1 deletion src/material-experimental/mdc-chips/chip-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class MatChipRow extends MatChip implements AfterContentInit, AfterViewIn
* Timeout used to give some time between `focusin` and `focusout`
* in order to determine whether focus has left the chip.
*/
private _focusoutTimeout: number | null;
private _focusoutTimeout: any;

constructor(
@Inject(DOCUMENT) private readonly _document: any,
Expand Down
2 changes: 1 addition & 1 deletion src/material-experimental/mdc-dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class MatDialogContainer extends _MatDialogContainerBase implements OnDes
private _closeAnimationDuration =
this._animationsEnabled ? numbers.DIALOG_ANIMATION_CLOSE_TIME_MS : 0;
/** Current timer for dialog animations. */
private _animationTimer: number|null = null;
private _animationTimer: any = null;

constructor(
elementRef: ElementRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class MatSnackBarContainer extends BasePortalOutlet
private readonly _announceDelay: number = 150;

/** The timeout for announcing the snack bar's content. */
private _announceTimeoutId: number;
private _announceTimeoutId: any;

/** Subject for notifying that the snack bar has announced to screen readers. */
readonly _onAnnounce: Subject<void> = new Subject();
Expand Down
2 changes: 1 addition & 1 deletion src/material/bottom-sheet/bottom-sheet-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class MatBottomSheetRef<T = any, R = any> {
private _result: R | undefined;

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

constructor(
containerInstance: MatBottomSheetContainer,
Expand Down
2 changes: 1 addition & 1 deletion src/material/dialog/dialog-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class MatDialogRef<T, R = any> {
private _result: R | undefined;

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

/** Current state of the dialog. */
private _state = MatDialogState.OPEN;
Expand Down
2 changes: 1 addition & 1 deletion src/material/snack-bar/snack-bar-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class MatSnackBarContainer extends BasePortalOutlet
private readonly _announceDelay: number = 150;

/** The timeout for announcing the snack bar's content. */
private _announceTimeoutId: number;
private _announceTimeoutId: any;

/** Whether the component has been destroyed. */
private _destroyed = false;
Expand Down
2 changes: 1 addition & 1 deletion src/material/snack-bar/snack-bar-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class MatSnackBarRef<T> {
* Timeout ID for the duration setTimeout call. Used to clear the timeout if the snackbar is
* dismissed before the duration passes.
*/
private _durationTimeoutId: number;
private _durationTimeoutId: any;

/** Whether the snack bar was dismissed using the action button. */
private _dismissedByAction = false;
Expand Down
6 changes: 3 additions & 3 deletions src/material/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export abstract class _MatTooltipBase<T extends _TooltipComponentBase> implement
private _document: Document;

/** Timer started at the last `touchstart` event. */
private _touchstartTimeout: number;
private _touchstartTimeout: any;

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

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

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

/** Property watched by the animation framework to show or hide the tooltip */
_visibility: TooltipVisibility = 'initial';
Expand Down
4 changes: 2 additions & 2 deletions tools/public_api_guard/material/tooltip.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ export abstract class _TooltipComponentBase implements OnDestroy {
_animationStart(): void;
_handleBodyInteraction(): void;
hide(delay: number): void;
_hideTimeoutId: number | undefined;
_hideTimeoutId: any;
isVisible(): boolean;
_markForCheck(): void;
message: string;
// (undocumented)
ngOnDestroy(): void;
protected _onShow(): void;
show(delay: number): void;
_showTimeoutId: number | undefined;
_showTimeoutId: any;
tooltipClass: string | string[] | Set<string> | {
[key: string]: any;
};
Expand Down