Skip to content
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

bug(cdk/overlay): incompatible to non-jasmine environment in testing #22926

Closed
lacolaco opened this issue Jun 9, 2021 · 2 comments · Fixed by #22927
Closed

bug(cdk/overlay): incompatible to non-jasmine environment in testing #22926

lacolaco opened this issue Jun 9, 2021 · 2 comments · Fixed by #22927
Assignees
Labels
area: cdk/testing P2 The issue is important to a large percentage of users, with a workaround regression This issue is related to a regression

Comments

@lacolaco
Copy link
Contributor

lacolaco commented Jun 9, 2021

Overlay is not supporting a testing environment that is not Jasmine-compatible.

See

const isTestEnvironment: boolean = typeof window !== 'undefined' && !!window &&
!!((window as any).__karma__ || (window as any).jasmine);

The isTestEnvironment flag must be true for the Overlay to be set up and tear down correctly in the test environment. This flag will only be true when running on Karma or using the Jasmine test runner.

This is unfriendly for users who use a testing tool like Jest to test Angular. Actually, until Jest v26, the default test runner was jest-jasmine2, which is a Jasmine compatible runner, so isTestEnvironment was true even for Jest. However, since Jest 27, the default test runner was changed to jest-circus, which is not Jasmine compatible, so testing of components using Overlay will fail.

Reproduction

Steps to reproduce:

  1. Clone https://github.com/lacolaco/jest-cdk-sandbox/
  2. Run yarn test:jasmine2 and it will be ok.
  3. Run yarn test:circus and it will fail.

The broken spec is below:

https://github.com/lacolaco/jest-cdk-sandbox/blob/cd65898cb907a549005e4214359fda1c12ebf179/src/app/app.component.spec.ts#L21-L33

As evidence to support this hypothesis, if I forge window.jasmine in Jest setup and pretend to be a Jasmine runner as the following workaround, this test will succeed. So this problem is not a problem with the internal workings of the new test runner, but simply a problem with the Overlay environment detection.

https://github.com/lacolaco/jest-cdk-sandbox/blob/cd65898cb907a549005e4214359fda1c12ebf179/src/setup-jest.ts#L3-L10

Object.defineProperty(window, 'jasmine', { value: true });

Expected Behavior

Keeping the same behavior as the Jasmine environment.

Actual Behavior

yarn test:circus only fails.

Environment

Angular CLI: 12.0.3
Node: 14.17.0
Package Manager: yarn 1.22.4
OS: darwin x64

Angular: 12.0.3
... animations, cdk, cli, common, compiler, compiler-cli, core
... forms, platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1200.3
@angular-devkit/build-angular   12.0.3
@angular-devkit/core            12.0.3
@angular-devkit/schematics      12.0.3
@schematics/angular             12.0.3
rxjs                            6.6.7
typescript                      4.2.4
@lacolaco lacolaco added the needs triage This issue needs to be triaged by the team label Jun 9, 2021
@mmalerba mmalerba added area: cdk/testing P2 The issue is important to a large percentage of users, with a workaround regression This issue is related to a regression and removed needs triage This issue needs to be triaged by the team labels Jun 9, 2021
@crisbeto
Copy link
Member

crisbeto commented Jun 9, 2021

For what it's worth, this was meant as a temporary solution until angular/angular#18831 is resolved and we can use ngOnDestroy to do the cleanup. It is something that we're actively working on now, but it'll take a while to become the default so I'll check if we can expand the test environment check.

@crisbeto crisbeto added the has pr label Jun 9, 2021
crisbeto added a commit to crisbeto/material2 that referenced this issue Jun 9, 2021
We have some logic in the overlay container that tries to prevent overlays from leaking between tests. The logic is currently limited to Jasmine tests and it happened to work by accident for Jest. Jest has made some changes that will break our check so these changes rework the logic to detect Jest and Mocha correctly.

Fixes angular#22926.
wagnermaciel pushed a commit that referenced this issue Jun 10, 2021
We have some logic in the overlay container that tries to prevent overlays from leaking between tests. The logic is currently limited to Jasmine tests and it happened to work by accident for Jest. Jest has made some changes that will break our check so these changes rework the logic to detect Jest and Mocha correctly.

Fixes #22926.
wagnermaciel pushed a commit that referenced this issue Jun 10, 2021
We have some logic in the overlay container that tries to prevent overlays from leaking between tests. The logic is currently limited to Jasmine tests and it happened to work by accident for Jest. Jest has made some changes that will break our check so these changes rework the logic to detect Jest and Mocha correctly.

Fixes #22926.

(cherry picked from commit 3f26e99)
@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 Jul 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: cdk/testing P2 The issue is important to a large percentage of users, with a workaround regression This issue is related to a regression
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants