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

Conversation

crisbeto
Copy link
Member

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.

@crisbeto crisbeto added P2 The issue is important to a large percentage of users, with a workaround target: patch This PR is targeted for the next patch release labels Sep 24, 2021
@crisbeto crisbeto requested a review from jelbourn September 24, 2021 15:56
@google-cla google-cla bot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Sep 24, 2021
Copy link
Member

@devversion devversion left a comment

Choose a reason for hiding this comment

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

@crisbeto crisbeto force-pushed the 23365/sanity-checks-test-again branch from 8e20836 to 81e467d Compare September 24, 2021 16:01
@crisbeto crisbeto requested a review from a team as a code owner September 24, 2021 16:01
@crisbeto crisbeto force-pushed the 23365/sanity-checks-test-again branch from 81e467d to be6774f Compare September 24, 2021 16:12
@@ -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).

@crisbeto crisbeto force-pushed the 23365/sanity-checks-test-again branch from be6774f to 100028e Compare September 24, 2021 16:14
…vironments

In angular#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 angular#23365.
@crisbeto crisbeto force-pushed the 23365/sanity-checks-test-again branch from 100028e to 52cba8d Compare September 24, 2021 18:05
@crisbeto crisbeto added the action: merge The PR is ready for merge by the caretaker label Sep 24, 2021
@josephperrott josephperrott removed the request for review from a team September 27, 2021 16:22
@michaelfaith
Copy link

Thanks for the follow-through on this.

@mmalerba mmalerba merged commit 4f6b9fd into angular:master Sep 30, 2021
mmalerba pushed a commit that referenced this pull request Sep 30, 2021
…vironments (#23636)

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.

(cherry picked from commit 4f6b9fd)
crisbeto added a commit to crisbeto/material2 that referenced this pull request Oct 10, 2021
In angular#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 angular#23636.

Fixes angular#23365.
crisbeto added a commit to crisbeto/material2 that referenced this pull request Oct 10, 2021
In angular#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 angular#23636.

Fixes angular#23365.
crisbeto added a commit to crisbeto/material2 that referenced this pull request Oct 25, 2021
In angular#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 angular#23636.

Fixes angular#23365.
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Oct 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement P2 The issue is important to a large percentage of users, with a workaround target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: Sanity checks run in test environement
4 participants