"Navigation triggered outside Angular zone" warning in unit tests
See original GitHub issueI’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:
- Created 5 years ago
- Reactions:127
- Comments:34 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Workaround:
In unit test: