-
Notifications
You must be signed in to change notification settings - Fork 26.4k
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
base: main
Are you sure you want to change the base?
Conversation
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. |
There was a problem hiding this 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.
37e39c7
to
16782c4
Compare
16782c4
to
9bbcadd
Compare
91d05d4
to
83634f9
Compare
83634f9
to
3b2f36e
Compare
/gemini review |
There was a problem hiding this 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.
this.route.queryParams.subscribe(params => { | ||
this.searchTerm = params['q'] || null; | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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; | |
}); | |
} |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
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?
Other information