docs: add draft dedicated router testing guide by bencodezen · Pull Request #62445 · angular/angular · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

docs: add draft dedicated router testing guide #62445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bencodezen
Copy link
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Adds a dedicated testing guide for router.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@angular-robot angular-robot bot added the area: docs Related to the documentation label Jul 2, 2025
@ngbot ngbot bot added this to the Backlog milestone Jul 2, 2025
@bencodezen bencodezen requested a review from atscott July 2, 2025 21:15
Copy link

github-actions bot commented Jul 2, 2025

Deployed adev-preview for 37e39c7 to: https://ng-dev-previews-fw--pr-angular-angular-62445-adev-prev-wbsw2qej.web.app

Note: As new commits are pushed to this pull request, this link is updated after the preview is rebuilt.

Copy link
Member

@JeanMeche JeanMeche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, I droped some drive-by comments.

import { Router } from '@angular/router';
import { Location } from '@angular/common';
import { Component } from '@angular/core';
import { provideRouter } from '@angular/router';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a duplicate import of @angular/router

{ path: 'about', component: MockAboutComponent }
])
]
}).compileComponents();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}).compileComponents();
})

This isn't required unless you have a @defer block + AOT: true.
Make sure to also remove the other ones.

imports: [RouterOutlet],
template: `
<h1>Parent Component</h1>
<router-outlet></router-outlet>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<router-outlet></router-outlet>
<router-outlet />

export class ParentComponent {}

@Component({
selector: 'app-child',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Routed components don't require selectors. We can probably omit it here.

private route = inject(ActivatedRoute);
searchTerm: string | null = null;

ngOnInit() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd argue to move that to the constructor since we don't have any dependency in inputs.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is big difference between ctor v.s. ngOnInit.
I would put no logic into the ctor.

Copy link
Member

@JeanMeche JeanMeche Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The recommendations have changed and no longer recommands to put init logic into the ngOnInit.

private route = inject(ActivatedRoute);
userId: string | null = null;

ngOnInit() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably move that to the constructor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not applied for some reason, the test itself should not need to call component.ngOnInit();.

<docs-code header="user-profile.component.spec.ts" language="typescript">
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';
import { UserProfileComponent } from './user-profile.component';
Copy link
Contributor

@michael-small michael-small Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the Component class suffix and .component suffix can be dropped from this and other imports/component names.

Also thank you for writing this guide. I am the worst with routing and testing respectively so this is great stuff to have

import { TestBed } from '@angular/core/testing';
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { authGuard } from './auth.guard';
import { AuthService } from './auth.service';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a follow up of @michael-small style guide recommandation, we could rename the service to AuthStore.


export const authGuard: CanActivateFn = () => {
const authService = inject(AuthService);
return authService.isAuthenticated();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return authService.isAuthenticated();
return inject(AuthService).isAuthenticated();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor for a single line

await TestBed.configureTestingModule({
imports: [UserProfileComponent],
providers: [
{ provide: ActivatedRoute, useValue: activatedRouteStub }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this I'd recommend to only test using RouterTestingHarness which should take care of most of the things.

I guess @atscott will agree to this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adev: preview area: docs Related to the documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

🎥 Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.