question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Navigation triggered outside Angular zone" warning in unit tests

See original GitHub issue

I’m submitting a…


[x] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

Current behavior

Using the last Angular 6.1.7 which introduced a warning if a navigation is triggered outside a zone (see https://github.com/angular/angular/commit/010e35d99596e1b35808aaaeecfc32f3b247a7d8), a simple unit test calling router.navigate yields the warning (resulting in hundreds of warnings in the console).

Expected behavior

The warning should not be emitted in unit tests. It’s currently possible to remove it by using zone.run or using NoopNgZone, but this is cumbersome. A way to disable it in unit tests would be great.

Minimal reproduction of the problem with instructions

Build a minimal app with the CLI

ng new router-warning --routing

Then replace the default unit test with:

import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { Router } from '@angular/router';

import { AppComponent } from './app.component';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));

  it('should create the app', async(() => {
    const router = TestBed.get(Router) as Router;
    router.navigateByUrl('/');
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));
});

Run ng test, and the following warning should appear:

WARN: 'Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?'

Environment


Angular version: 6.1.7 or 7.0.0-beta.5

Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: 8.11.3
- Platform:  Mac

cc @trotyl

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:127
  • Comments:34 (6 by maintainers)

github_iconTop GitHub Comments

99reactions
ItzhakBokriscommented, Oct 29, 2018

Workaround:

constructor(private ngZone: NgZone, private router: Router) {}

public navigate(commands: any[]): void {
    this.ngZone.run(() => this.router.navigate(commands)).then();
}
77reactions
CharlyRippcommented, Oct 30, 2018

In unit test:

it('should just work', async(() => {
    fixture.ngZone.run(() => {
        router.navigate(['path1']);
        fixture.detectChanges();
        fixture.whenStable().then(() => {
            expect({stuff}).toHaveBeenCalled();
        });
    });
}));
Read more comments on GitHub >

github_iconTop Results From Across the Web

WARN: 'Navigation triggered outside Angular zone, did you ...
I want to write unit test to see if this API is called or not using HttpTestingModule,which it does, but in the code...
Read more >
did you forget to call ngZone.run()? - Coditty
navigate but Angular does not navigate correctly to that route but throws a warning in console "Navigation triggered outside Angular zone, did you...
Read more >
Testing Angular routing components with the ...
As Angular issue #25837 discusses, Angular outputs a warning when we trigger navigation outside of a test case – usually in beforeEach hooks. ......
Read more >
Navigation triggered outside Angular zone' explanation ...
Angular unit test :WARN: 'Navigation triggered outside Angular zone, did you forget to call 'ngZone.run()'?' canDeactivate() is not triggered on page navigation ...
Read more >
Component testing scenarios - Angular
To use fakeAsync() functionality, you must import zone.js/testing in your test setup file. If you created your project with the Angular CLI, zone-testing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found