Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Run the schematic

  1. From the Angular project, run ng generate @angular/core:router-testing-module-migration. The command is listed in Angular’s migration catalog.
  2. To migrate only part of the project, pass the schematic’s path option with a path relative to the project root. It defaults to ./, which selects the project root.
  3. 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:

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.