iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Angular’s official migration schematic replaces test uses of RouterTestingModule with RouterModule; it does not universally convert them to provideRouter. Run ng generate @angular/core:router-testing-module-migration, then review the changes to confirm that route configuration and any location-mock behavior your tests rely on are preserved.
What the RouterTestingModule migration changes
Angular’s Router Testing Module Migration describes a schematic that migrates uses of RouterTestingModule in tests to RouterModule. Its documented example replaces RouterTestingModule.withRoutes(...) with RouterModule.forRoot(...). The schematic also adds location mocks in a specific case involving SpyLocation.urlChanges.
RouterTestingModule is labeled deprecated in Angular’s v18 API reference. Use the documented schematic for this migration, but check your project’s Angular version and inspect its output rather than assuming every test setup will be transformed the same way.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallRun the schematic
- From the Angular project, run
ng generate @angular/core:router-testing-module-migration. The command is listed in Angular’s migration catalog. - To migrate only part of the project, pass the schematic’s
pathoption with a path relative to the project root. It defaults to./, which selects the project root. - Review the edited tests and run the relevant test suite. Check that imports, route configuration, and any assertions involving location behavior still match the test’s intent.
How route configuration is carried over
Angular’s documented example preserves the configured routes and the demonstrated initialNavigation option when changing from RouterTestingModule.withRoutes to RouterModule.forRoot. For example, the before form is:
#1 Best Overall
RouterTestingModule.withRoutes(routes, {initialNavigation: 'enabledBlocking'})
The documented after form imports RouterModule from @angular/router and uses:
RouterModule.forRoot(routes, {initialNavigation: 'enabledBlocking'})
Keep the route array and applicable options from your own test setup. The example establishes this mapping for its shown configuration; it does not establish that every test needs the same options or that every legacy configuration is handled identically.
Rank #2
When the migration adds location mocks
The schematic adds provideLocationMocks() when a test imports SpyLocation from @angular/common/testing and uses its urlChanges property. Angular’s example places provideLocationMocks() in the TestBed providers and imports it from @angular/common/testing.
Free tools Windows power users keep installed
One-click scans. No signup required.
After migration, check tests that assert on URL changes and verify that the location-mock provider is present where needed. This documented condition is specific: it is not a reason to add provideLocationMocks() to every test that uses routing.
Rank #3
RouterModule output versus provideRouter
provideRouter is Angular’s provider-based router setup, with feature helpers for router configuration and initial navigation. It can be relevant in applications and tests that use environment providers, while RouterModule.forRoot fits the module-based form shown in the migration example.
| Consideration | Documented schematic example | Provider-based setup |
|---|---|---|
| Setup style | Uses RouterModule.forRoot, an NgModule-based API. |
Uses provideRouter and provider configuration; verify available APIs for your Angular version. |
| Routes and options | The example carries the route array and initialNavigation: 'enabledBlocking' into RouterModule.forRoot. |
Choose the provider configuration and feature helpers needed by the test; the migration example does not prescribe a complete conversion. |
| Location behavior | The schematic adds provideLocationMocks() for the documented SpyLocation.urlChanges case. |
Check whether the test depends on that behavior and configure providers accordingly. |
| Version fit | Follow the schematic documentation and confirm it is available for your project’s Angular version. | Consult the router API for the version your project uses; the linked implementation is on Angular’s main branch and may differ from released versions. |
Do not treat a provider-based setup as the schematic’s guaranteed output. If your test suite already uses provideRouter, assess each test’s existing configuration and migrate it consistently with the APIs supported by your Angular version rather than mechanically applying the module-based example.
Quick Recap
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Recommended Free Tools

