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

NoopNgZone for unit tests #43549

Closed
sod opened this issue Sep 23, 2021 · 2 comments
Closed

NoopNgZone for unit tests #43549

sod opened this issue Sep 23, 2021 · 2 comments
Assignees
Labels
area: testing Issues related to Angular testing features, such as TestBed area: zones P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Milestone

Comments

@sod
Copy link
Contributor

sod commented Sep 23, 2021

Which @angular/* package(s) are relevant/releated to the feature request?

core, platform-browser-dynamic

Description

The introduction of NoopNgZone a few years ago was awesome ❤️ . But it was never ported to unit tests.

There is no good reason why I want this other than my distaste in the implementation of zone.js. Rxjs + async pipe + Input push change detection is perfectly fine. There is no need for 60kb of monkeypatching half the environment IMO. Maybe nicer stack traces is a plus. Or being able to use any testrunner you want without the need of extra patches.

So feel free to close if this feature wont happen.

Proposed solution

Don't hardcode in the testing modules the assumption that Zone exists and allow the use of NoopNgZone

Alternatives considered

Our current solution is a file angular-testing-noop-zone.ts with contents:

const noop = () => {};
const identity = (value: unknown) => value;
const Zone = {
    __symbol__: identity,
    assertZonePatched: noop,
    current: {
        fork: noop,
        get: identity,
    },
    fakeAsyncTest: {
        resetFakeAsyncZone: noop,
    },
};

// global.Zone has to exist as the testing modules access some functions directly instead of using the TestEnvironment dependency injection
Object.assign(global, {Zone});

const {NgZone, ɵNoopNgZone} = require('@angular/core');
const {getTestBed} = require('@angular/core/testing');
const {BrowserDynamicTestingModule, platformBrowserDynamicTesting} = require('@angular/platform-browser-dynamic/testing');

// NgZone has to be bend to noop as both core-testing and platform-browser-testing do `new core.NgZone` instead of using the TestEnvironment dependency injection
Object.assign(NgZone.prototype, ɵNoopNgZone.prototype);

getTestBed().initTestEnvironment(
    BrowserDynamicTestingModule,
    platformBrowserDynamicTesting([{provide: NgZone, useValue: new ɵNoopNgZone()}]),
);

We use jest as a test runner and native async/await. As jest isn't officially supported by zone.js, you have to use the patch from jest-preset-angular or bake your own. The above file renders that patch obsolete as the environment is untouched by zone.js. jest just runs as intended.

@pkozlowski-opensource pkozlowski-opensource added area: testing Issues related to Angular testing features, such as TestBed area: zones labels Sep 28, 2021
@ngbot ngbot bot added this to the needsTriage milestone Sep 28, 2021
@JiaLiPassion JiaLiPassion self-assigned this Feb 20, 2022
@alxhub alxhub added the P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent label Nov 16, 2022
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Nov 16, 2022
@pkozlowski-opensource
Copy link
Member

Duplicate of #48198 - closing this one as there is more discussion in #48198

@pkozlowski-opensource pkozlowski-opensource closed this as not planned Won't fix, can't repro, duplicate, stale Mar 26, 2024
@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 Apr 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: testing Issues related to Angular testing features, such as TestBed area: zones P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

No branches or pull requests

4 participants