Skip to content

docs: add 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

Open
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 3b2f36e 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.

@bencodezen bencodezen force-pushed the docs/router-testing-guide branch from 37e39c7 to 16782c4 Compare July 10, 2025 19:28
@bencodezen bencodezen added the action: review The PR is still awaiting reviews from at least one requested reviewer label Jul 10, 2025
@bencodezen bencodezen requested a review from atscott July 10, 2025 19:29
@bencodezen bencodezen changed the title docs: add draft dedicated router testing guide docs: add dedicated router testing guide Jul 10, 2025
@bencodezen bencodezen marked this pull request as ready for review July 10, 2025 19:29
@bencodezen bencodezen force-pushed the docs/router-testing-guide branch from 16782c4 to 9bbcadd Compare July 10, 2025 19:30
@bencodezen bencodezen force-pushed the docs/router-testing-guide branch 2 times, most recently from 91d05d4 to 83634f9 Compare July 14, 2025 18:51
@bencodezen bencodezen requested a review from atscott July 14, 2025 18:52
@bencodezen bencodezen force-pushed the docs/router-testing-guide branch from 83634f9 to 3b2f36e Compare July 15, 2025 18:28
@bencodezen bencodezen requested a review from JeanMeche July 15, 2025 18:28
@atscott
Copy link
Contributor

atscott commented Jul 15, 2025

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a new guide for testing routing and navigation in Angular applications. The guide covers route parameters, guards, router outlets, nested routes, and query parameters. It also includes best practices for router testing.

Comment on lines +342 to +344
this.route.queryParams.subscribe(params => {
this.searchTerm = params['q'] || null;
});

Choose a reason for hiding this comment

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

high

The subscription to queryParams is not unsubscribed, which can lead to a memory leak if the component is destroyed before the observable completes. Use takeUntilDestroyed to automatically unsubscribe when the component is destroyed.

Suggested change
this.route.queryParams.subscribe(params => {
this.searchTerm = params['q'] || null;
});
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
constructor() {
this.route.queryParams.pipe(takeUntilDestroyed()).subscribe(params => {
this.searchTerm = params['q'] || null;
});
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably want toSignal instead


## Best practices for router testing

1. **Use RouterTestingHarness** - For testing routed components, use [`RouterTestingHarness`](api/router/testing/RouterTestingHarness) which provides a cleaner API and eliminates the need for test host components. It offers direct component access, built-in navigation, and better type safety.
Copy link
Contributor

Choose a reason for hiding this comment

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

You can maybe mention that RouterTestingHarness isn't suitable for all advanced use-cases and you might instead need to create your own host components for named outlets, for example.

3. **Test navigation state** - Verify both the navigation action and the resulting application state, including URL changes and component rendering.
4. **Handle asynchronous operations** - Router navigation is asynchronous. Use `async/await` or [`fakeAsync`](api/core/testing/fakeAsync) to properly handle timing in your tests.
5. **Test error scenarios** - Include tests for invalid routes, failed navigation, and guard rejections to ensure your application handles edge cases gracefully.
6. **Do not mock Angular router.** Instead, provide real route configurations and use the harness to navigate. This makes your tests more robust and less likely to break on internal Angular updates.
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
6. **Do not mock Angular router.** Instead, provide real route configurations and use the harness to navigate. This makes your tests more robust and less likely to break on internal Angular updates.
6. **Do not mock Angular router.** Instead, provide real route configurations and use the harness to navigate. This makes your tests more robust and less likely to break on internal Angular updates.

And/or more likely to catch real issues due to changes in Angular. e.g. if the Router behavior changes in a way that breaks your application, a mock would prevent you from observing that breaking change

## Best practices for router testing

1. **Use RouterTestingHarness** - For testing routed components, use [`RouterTestingHarness`](api/router/testing/RouterTestingHarness) which provides a cleaner API and eliminates the need for test host components. It offers direct component access, built-in navigation, and better type safety.
2. **Mock external dependencies** - When testing components that use routing, mock any external services or dependencies to focus your tests on routing behavior.
Copy link
Contributor

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action: review The PR is still awaiting reviews from at least one requested reviewer adev: preview area: docs Related to the documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants