Router events triggered unexpectedly when a component is rendered
See original GitHub issueThe router events will be trigger when a component is rendered.
Given a component
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-apple',
templateUrl: './apple.component.html',
styleUrls: ['./apple.component.css'],
})
export class AppleComponent implements OnInit {
constructor(private router: Router) {}
ngOnInit(): void {
this.router.events.subscribe((event) => {
console.log(event);
});
}
}
and its test
import { AppleComponent } from './apple.component';
import { render, screen } from '@testing-library/angular'
import { RouterTestingModule } from '@angular/router/testing';
describe('AppleComponent', () => {
it('should have "apple works!"', async () => {
await render(AppleComponent, {
declarations: [AppleComponent],
imports: [RouterTestingModule]
})
expect(screen.getByText('apple works!'));
});
});
Now run the test then you can see the router events (which won’t show up with Angular TestBed or when running the app)
Is this an intentional behaviour? (FYI I believe this behaviour was introduced in version 11.0.3: https://github.com/testing-library/angular-testing-library/pull/286)
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
this.router.events not loading on first time in Angular
The NavigationEnd event has already been raised when the component registers for it. best solution would be to use the rxjs startWith ...
Read more >useNavigate hook causes waste rendering #7634 - GitHub
We have a field with an onComplete handler that, when triggered, navigates to a new route. A custom input component receives an onComplete...
Read more >Event - @angular/router
Router events that allow you to track the lifecycle of the router. ... NavigationError: When navigation fails due to an unexpected error.
Read more >React Components rendered twice — any way to fix this?
The reason why this happens is an intentional feature of the React.StrictMode . It only happens in development mode and should help to...
Read more >Angular router navigation events - YouTube
In this video we will discuss the sequence of navigation events that are triggered by the angular router, when navigating from one route...
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 Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thanks @ruoxin-mx51, this will get fixed soon.
🎉 This issue has been resolved in version 13.0.0 🎉
The release is available on:
Your semantic-release bot 📦🚀