Where is MockNgZone for Angular 4
Clash Royale CLAN TAG#URR8PPP
Where is MockNgZone for Angular 4
Angular 2 provided a mock implementation of NgZone called MockNgZone. This doesn't appear to be available in Angular 4. Does anyone know where or why it has gone?
@angular/core/testing
@jonrsharpe Great. But then why can't I seem to import it using
import MockNgZone from "@angular/core/testing"
?– James B
May 29 '17 at 18:08
import MockNgZone from "@angular/core/testing"
Because it is not private api anymore.
core/testing: Remove the following APIs from @angular/core/testing, which have been deprecated or were never intended to be publicly exported:
github.com/angular/angular/blob/master/…– yurzui
May 29 '17 at 18:11
core/testing: Remove the following APIs from @angular/core/testing, which have been deprecated or were never intended to be publicly exported:
A question likelier to solve your problem might be: given the following usage of NgMockZone, how can I do this in Angular 4.x?
– jonrsharpe
May 29 '17 at 18:25
1 Answer
1
Overview: Angular 4/5/6 still define MockNgZone
within @angular/core/testing/src/ng_zone_mock, however it is not exported publicly. Created December 2017, Issue #21075: Expose MockNgZone is still open on Angular's GitHub account. Pull Request #21628 was started January 2018 and exposes MockNgZone but the PR has never been completed, stalled with a merge conflict since March 2018.
MockNgZone
Option 1: Re-create MockNgZone
as a testing utility for yourself.
MockNgZone
import EventEmitter, Injectable, NgZone from '@angular/core';
@Injectable()
export class MockNgZone extends NgZone
onStable: EventEmitter<any> = new EventEmitter(false);
constructor() super(enableLongStackTrace: false);
run(fn: Function): any return fn();
runOutsideAngular(fn: Function): any return fn();
simulateZoneExit(): void this.onStable.emit(null);
Option 2: Angular does publicly export a similar NoopNgZone
from the same location as NgZone
however the implementation is different from MockNgZone
.
NoopNgZone
NgZone
MockNgZone
import NgZone from '@angular/core';
import NoopNgZone from '@angular/core/src/zone/ng_zone';
TestBed.configureTestingModule(
providers: [ provide: NgZone, useClass: NoopNgZone ]
)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It's still there, in
@angular/core/testing
: github.com/angular/angular/blob/4.0.x/packages/core/testing/src/…– jonrsharpe
May 29 '17 at 18:05