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.

Router events triggered unexpectedly when a component is rendered

See original GitHub issue

The 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:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
timdeschryvercommented, Nov 20, 2022

Thanks @ruoxin-mx51, this will get fixed soon.

0reactions
github-actions[bot]commented, Dec 14, 2022

🎉 This issue has been resolved in version 13.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Read more comments on GitHub >

github_iconTop 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 >

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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found