James B
James B

Reputation: 9605

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?

Upvotes: 1

Views: 1413

Answers (1)

Kyle Whitaker
Kyle Whitaker

Reputation: 173

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.

Option 1: Re-create MockNgZone as a testing utility for yourself.

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.

import { NgZone } from '@angular/core';
import { NoopNgZone } from '@angular/core/src/zone/ng_zone';

TestBed.configureTestingModule({
  providers: [{ provide: NgZone, useClass: NoopNgZone }]
})

Upvotes: 2

Related Questions