October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Migrate Angular Tests from RouterTestingModule

Angular’s schematic migrates RouterTestingModule tests to RouterModule, preserves configured routes and options, and adds location mocks for the documented SpyLocation urlChanges case.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Angular’s official migration schematic replaces test uses of RouterTestingModule with RouterModule. Run ng generate @angular/core:router-testing-module-migration; it can also add provideLocationMocks() when a test imports SpyLocation and reads urlChanges. Its documented conversion is module-based, not a universal rewrite to provideRouter.

Run the RouterTestingModule migration

From the Angular workspace, run:

ng generate @angular/core:router-testing-module-migration

The schematic migrates RouterTestingModule usages inside tests to RouterModule. To target only part of a project, pass --path with a path relative to the project root. The option defaults to ./, so you can migrate a subset before running it across the workspace. See Angular’s Router Testing Module Migration guide.

As an Amazon Associate I earn from qualifying purchases.

What the schematic changes

Route configuration and options

For a test using RouterTestingModule.withRoutes(routes, options), Angular documents replacing it with RouterModule.forRoot(routes, options). The route array and options are carried into the new call. For example, a test configured with blocking initial navigation becomes:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import { RouterModule } from '@angular/router';

TestBed.configureTestingModule({
  imports: [
    RouterModule.forRoot(routes, { initialNavigation: 'enabledBlocking' }),
  ],
});

This preserves the demonstrated initialNavigation setting as well as the routes. Review the migrated test’s imports and configuration if it uses other options; do not assume an unrelated provider-based rewrite has occurred. The before-and-after pattern is shown in the migration guide.

Tests that inspect URL changes

Angular documents a specific location-mock case: when a test imports SpyLocation from @angular/common/testing and uses its urlChanges property, the schematic adds provideLocationMocks() to the TestBed providers to preserve that behavior. The helper is imported from @angular/common/testing:

import { provideLocationMocks } from '@angular/common/testing';

TestBed.configureTestingModule({
  providers: [provideLocationMocks()],
});

This is a targeted compatibility measure, not a requirement for every test that uses routing. Add or retain location mocks when the test relies on the mock location behavior, particularly the documented SpyLocation.urlChanges use.

RouterModule migration versus provideRouter

provideRouter configures routing through providers and has feature helpers for settings such as initial navigation and router configuration. It can fit applications and tests built around environment providers, but Angular’s migration example maps RouterTestingModule.withRoutes to RouterModule.forRoot; it does not prescribe a complete conversion of every test to provideRouter. Check the API available in the project’s Angular version and match the test’s existing setup.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Question Documented RouterModule migration Provider-based setup
How routing is configured Imports RouterModule.forRoot(routes, options) in the test module. Provides the router with provideRouter and applicable feature helpers.
Where routes and options go The documented example carries the route array and ExtraOptions into forRoot. Configure routes and features using APIs supported by the project’s Angular version; the migration example does not prescribe a universal mapping.
Location behavior The schematic adds provideLocationMocks() for its documented SpyLocation.urlChanges case. Keep location mocks if the test depends on that behavior; router providers alone are not a substitute for the documented mock.
Best fit Tests already using NgModule imports and the schematic’s documented replacement. Tests organized around provider-based or standalone configuration, after verifying version compatibility.

The provider helpers are documented in Angular’s router provider API source. That URL tracks the main branch, so confirm the APIs against the release used by your project.

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

Deprecation and version context

Angular’s v18 API reference labels RouterTestingModule deprecated and lists withRoutes(routes, config?). The current migration catalog describes replacing its test usages with RouterModule, with location mocks added when the documented condition applies. Use the migration path supported by your Angular version rather than treating the deprecation label as evidence that every project should adopt the same provider configuration. See the Angular v18 RouterTestingModule API reference and Angular migrations overview.

Verify the migrated tests

  1. Run the schematic at the workspace root, or use --path to start with a project subset.
  2. Inspect the changed TestBed imports and providers. Confirm routes and options, including initial-navigation settings, remain present.
  3. Check tests that import SpyLocation and use urlChanges for the generated provideLocationMocks() provider.
  4. Run the affected test suite. If a test uses provider-based setup or other router features, verify those APIs against the project’s Angular release and adapt that test deliberately rather than assuming the schematic converted it.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.