You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Automatic migration for RouterTestingModule to provideRouter([]) or RouterModule #54853
Which @angular/* package(s) are relevant/related to the feature request?
router
Description
If a project upgrades to Angular 17.3.0, RouterTestingModule's deprecation (#54466) will cause a ton of warnings/errors if that project has linter rules against deprecated APIs. Can Angular provide an official migration for this?
Reacted by Adrien, Rob, Adam Mouaddine, onyzhin, Frederico Jesus, Agemiro Alves de Sousa Neto, Joseph Kastier, Johannes Fahrenkrug, Jakub Marinčič, Timothy Lombrana and 6 more
Reacted by Csaba-Gyozo, Stoyan Dobrev and João Vitor Veronese Vieira
There will be a migration before the api is removed, which could be v19 or later. It’s not a high priority item for the team so a community contribution would speed things up here.
The most straightforward thing to do would be to directly replace with RouterModule.forRoot in all cases. To keep more tests passing, provideLocationMocks could be added to providers but it’s really be better to update test where possible instead. Some tests may fail for a couple reasons:
expected location path to be ‘’ but was ‘/‘ (or the other way around, I don’t recall offhand). I would recommend just updating those assertions.
your test depends on SpyLocation. You would need to add provideLocationMocks to the TestBed providers for this or decide how to remove the dependency.
Can you do a find/replace in your project and report back with the results?
I'm not generally doing a lot of unit testing around specific routing features, mostly just needing the import to ensure [routerLink] is a known directive. So nearly everything in my particular use case got replaced with just RouterModule, and areas that complained about missing ActivatedRoute I used RouterModule.forRoot([]) instead.
Reacted by Nino Verstraeten, aYorky and David Silva
In a similar situation, replaced RouterTestingModule with providerRouter([]) in 113 test files, and only 1 had an issue where I am using locationSpy to test changes to a URL hash. this is the test:
it('can navigate by time without triggering route change',()=>{constlocationSpy=spectator.inject(Location);constrouterSpy=spectator.inject(Router);jest.spyOn(locationSpy,'go');jest.spyOn(routerSpy,'navigate');spectator.service.navigateToTime(0.5);expect(locationSpy.go).toHaveBeenLastCalledWith('#t=0.5');expect(routerSpy.navigate).not.toHaveBeenCalled();spectator.service.navigateToTime(10.123456789);expect(locationSpy.go).toHaveBeenLastCalledWith('/#t=10.123457');spectator.service.navigateToTime(1);expect(locationSpy.go).toHaveBeenLastCalledWith('/#t=1');spectator.service.navigateToTime(0);expect(locationSpy.go).toHaveBeenLastCalledWith('/');expect(routerSpy.navigate).not.toHaveBeenCalled();});
this is the test result:
VideoSyncService can navigate by time without triggering route change [3 ms] Debug test | Open test story
Expected: "/#t=10.123457"
Received
1: "#t=0.5"
-> 2: "#t=10.123457"
Number of calls: 2
at projects/artella-lib/src/lib/video/video-sync.service.spec.ts:72:5
This test passes when using RouterTestingModule.
Just reporting this in answer to @atscott "report back with the results?" — I'm going to look for a solution (which is what lead me here ~:)
Update: I've worked around this by mocking Location
@bboyle thanks for the info. The depreciation note does mention this case:
This module was previously used to provide a helpful collection of test fakes, most notably those for Location and LocationStrategy. These are generally not required anymore, as MockPlatformLocation is provided in TestBed by default. However, you can use them directly with provideLocationMocks.
thanks @atscott, sounds very helpful I'll check it out! I'm not sure where the deprecation note is? I got informed by vscode/typescript that it was deprecated, rather than having read release notes in this instance.
update: providerRouter(), provideLocationMocks() works perfectly as a drop-in replacement for RouterTestingModule in that test! 🎊
Why doesn't provideRouter supply the routerLink directive? It seems much more appropriate to use a provide function than import a whole module in a test.
I'm taking his so I can generate a new migration.
I would consider using RouterModule for simplicity if there's an integration test and I need RouterLink, so I don't have to analyze the component or components on which the test or behavior depends.
For location cases and their mocks, I think it's enough to check if the imports exist to replace them.
Activity
atscott commentedon Mar 14, 2024
There will be a migration before the api is removed, which could be v19 or later. It’s not a high priority item for the team so a community contribution would speed things up here.
The most straightforward thing to do would be to directly replace with RouterModule.forRoot in all cases. To keep more tests passing, provideLocationMocks could be added to providers but it’s really be better to update test where possible instead. Some tests may fail for a couple reasons:
Can you do a find/replace in your project and report back with the results?
JasonWeinzierl commentedon Mar 14, 2024
I did a find/replace in one of my public projects: JasonWeinzierl/BHS.NET@31f812c
I'm not generally doing a lot of unit testing around specific routing features, mostly just needing the import to ensure
[routerLink]is a known directive. So nearly everything in my particular use case got replaced with justRouterModule, and areas that complained about missingActivatedRouteI usedRouterModule.forRoot([])instead.bboyle commentedon Apr 18, 2024
In a similar situation, replaced RouterTestingModule with providerRouter([]) in 113 test files, and only 1 had an issue where I am using locationSpy to test changes to a URL hash. this is the test:
this is the test result:
This test passes when using RouterTestingModule.
Just reporting this in answer to @atscott "report back with the results?" — I'm going to look for a solution (which is what lead me here ~:)
Update: I've worked around this by mocking
Locationand updating the tests to look for that, e.g.
atscott commentedon Apr 18, 2024
@bboyle thanks for the info. The depreciation note does mention this case:
bboyle commentedon Apr 18, 2024
thanks @atscott, sounds very helpful I'll check it out! I'm not sure where the deprecation note is? I got informed by vscode/typescript that it was deprecated, rather than having read release notes in this instance.
update:
providerRouter(), provideLocationMocks()works perfectly as a drop-in replacement for RouterTestingModule in that test! 🎊tingchongma15 commentedon Jun 16, 2024
@atscott, @bboyle,
Is the deprecation note at:
@deprecated at:
https://github.com/angular/angular/blob/main/packages/router/testing/src/router_testing_module.ts
provideRouterorRouterModule/RouterModule.forRootinstead.LocationandLocationStrategy. These are generally notMockPlatformLocationis provided inTestBedby default.provideLocationMocks.?
Replace `RouterTestingModule` with `RouterModule`
Replace `RouterTestingModule` with `RouterModule`
dmrickey commentedon Jan 29, 2025
Why doesn't
provideRoutersupply therouterLinkdirective? It seems much more appropriate to use a provide function than import a whole module in a test.JeanMeche commentedon Jan 30, 2025
provideRouteris a provider function that only defines providers and doesn't do the necessary import in standalone components.Replace deprecated RouterTestingModule by RouterModule.forRoot([])
[sc-12483] update angular to fix dependabot alerts (#2197)
SkyZeroZx commentedon Oct 2, 2025
I'm taking his so I can generate a new migration.
I would consider using RouterModule for simplicity if there's an integration test and I need RouterLink, so I don't have to analyze the component or components on which the test or behavior depends.
For location cases and their mocks, I think it's enough to check if the imports exist to replace them.
atscott commentedon Oct 14, 2025
Fixed by #64217
angular-automatic-lock-bot commentedon Nov 14, 2025
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.