Skip to content

Automatic migration for RouterTestingModule to provideRouter([]) or RouterModule #54853

Closed

Description

@JasonWeinzierl

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?

Proposed solution

Automatically migrate from this:

imports: [
  ...
  RouterTestingModule.withRoutes([]),
  ...
],

to this if the component is not standalone:

imports: [
  ...
  RouterModule.forRoot([]),
  ...
],

or to this if the component is standalone:

providers: [
  ...
  provideRouter([]),
  ...
],

and fix the relevant import statements.

Alternatives considered

Manually migrate everything.

Activity

  1. added this to the needsTriage milestone on Mar 14, 2024
  2. atscott commented on Mar 14, 2024

    @atscott
    Contributor

    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?

  3. JasonWeinzierl commented on Mar 14, 2024

    @JasonWeinzierl
    Author

    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 just RouterModule, and areas that complained about missing ActivatedRoute I used RouterModule.forRoot([]) instead.

  4. bboyle commented on Apr 18, 2024

    @bboyle

    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', () => {
        const locationSpy = spectator.inject(Location);
        const routerSpy = 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

        providers: [
          provideRouter([]),
          MockProvider(Location, { go: () => {}, path: () => '<path>' }),
        ],

    and updating the tests to look for that, e.g.

        expect(locationSpy.go).toHaveBeenLastCalledWith('<path>#t=0.5');
        …
        expect(locationSpy.go).toHaveBeenLastCalledWith('<path>#t=10.123457');
    
  5. atscott commented on Apr 18, 2024

    @atscott
    Contributor

    @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.

  6. bboyle commented on Apr 18, 2024

    @bboyle

    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! 🎊

  7. tingchongma15 commented on Jun 16, 2024

    @tingchongma15

    @atscott, @bboyle,
    Is the deprecation note at:
    @deprecated at:
    https://github.com/angular/angular/blob/main/packages/router/testing/src/router_testing_module.ts

    • @publicapi
    • @deprecated Use provideRouter or RouterModule/RouterModule.forRoot instead.
    • 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.
      ?
  8. dmrickey commented on Jan 29, 2025

    @dmrickey

    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.

  9. JeanMeche commented on Jan 30, 2025

    @JeanMeche
    Member

    provideRouter is a provider function that only defines providers and doesn't do the necessary import in standalone components.

  10. added
    open for contributions
    on Sep 23, 2025
  11. SkyZeroZx commented on Oct 2, 2025

    @SkyZeroZx
    Contributor

    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.

  12. atscott commented on Oct 14, 2025

    @atscott
    Contributor

    Fixed by #64217

  13. angular-automatic-lock-bot commented on Nov 14, 2025

    @angular-automatic-lock-bot

    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.

  14. locked and limited conversation to collaborators on Nov 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: routeropen for contributions

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions