angular icon indicating copy to clipboard operation
angular copied to clipboard

docs: RouterTestingModule should be added to Testing guide

Open Splaktar opened this issue 5 years ago • 27 comments

📚 Docs or angular.io bug report

Description

The Testing Guide has the following "helpful" alert:

A future guide update will explain how to write such tests with the RouterTestingModule.

This was added as part of PR https://github.com/angular/angular/pull/20697 in 2017. Since this hasn't been addressed yet, I figured it would be a good idea to open an issue to ensure that it gets documented.

There are some minimal RouterTestingModule API docs but they are not sufficient. This is something that really needs to be included in the Testing Guide. Both for Services that cause routing and components.

🔬 Minimal Reproduction

What's the affected URL?**

Guide: https://angular.io/guide/testing#what-good-are-these-tests Source: https://github.com/angular/angular/blame/fe4447d56860c55000bc7424a61462b6de8596e5/aio/content/guide/testing.md#L2250-L2251

Splaktar avatar Dec 11 '19 05:12 Splaktar

Agreed on the "helpful" note. According to guidelines it's ideal for documentation not to make references to what "should" come in the future. Instead, we're supposed to stick with what we're documenting in the present moment.

Note for docs team: when this becomes a PR, we'll need to remove this note and sweep for any other future-related notes in there.

kapunahelewong avatar Dec 11 '19 20:12 kapunahelewong

@kapunahelewong can I take this issue

ajitsinghkaler avatar Mar 09 '20 15:03 ajitsinghkaler

Yes, @ajitsinghkaler, thank you!! Please let me know if you need help with any of the issues you've volunteered for. 🌟

kapunahelewong avatar Mar 10 '20 18:03 kapunahelewong

The alert seems to be directly related to the preceding paragraph:

A different battery of tests can explore whether the application navigates as expected in the presence of conditions that influence guards such as whether the user is authenticated and authorized.

What's the scope for this issue? Adding a guide for different types of tests using the RouterTestingModule or only integration tests exercising conditions affected by route guards?

LayZeeDK avatar Jun 09 '20 22:06 LayZeeDK

Good questions, @LayZeeDK. These are exactly the kinds of questions we are discussing with in the docs team as we rework a lot of the documentation. Do you have any particular thoughts on the scope and types of example tests that you'd be interested in? If you search for something regarding testing what are the topics that first come to mind? Your use cases would also be interesting to hear. If you or others would like to log your thoughts here in this issue I'd be happy to link to this issue from the Jira as context for the writer.

kapunahelewong avatar Jun 10 '20 19:06 kapunahelewong

Different types of tests involving the RouterTestingModule:

  • Testing routed components
  • Testing routing components
  • Testing route guards
  • Testing route resolvers
  • Integration testing routed feature modules/standalone routes
  • Integration testing routed feature modules/standalone routes with route guards
  • Integration testing a routed component with nested routes
  • Integration testing a routed component with route resolvers

LayZeeDK avatar Jun 10 '20 19:06 LayZeeDK

I've been intensively looking into this 3 year old issue for the past week: https://github.com/angular/angular/issues/15779

I feel more and more convinced that for the most part we don't need to extend our testing APIs to close it, but rather write a guide covering the types of tests listed above.

LayZeeDK avatar Jun 10 '20 20:06 LayZeeDK

Thank you, @LayZeeDK! I've added this and #15779 to the Jira work item so the writer who works on it can refer back for context.

Docs team: See https://angular-team.atlassian.net/browse/DOCS-784

kapunahelewong avatar Jun 15 '20 13:06 kapunahelewong

I've been intensively looking into this 3 year old issue for the past week: #15779

I feel more and more convinced that for the most part we don't need to extend our testing APIs to close it, but rather write a guide covering the types of tests listed above.

Yes, the update to the testing guide for routing should include details on how to provide snapshot data for use via ActivatedRoute.snapshot and ActivatedRouteSnapshot. This is discussed in #15779.

@LayZeeDK thank you for helping move this issue forward!

Splaktar avatar Jul 12 '20 03:07 Splaktar

I'm working on another article on unit and integration testing route guards. The isolated unit tests show that it could be nice to have helpers to create the complex data structures that we need to pass to route guards and route resolvers.

Example solutions 2 and 3 in this comment also show that routing data structure helpers could be useful for testing routed components in both isolated component tests and isolated unit tests.

LayZeeDK avatar Jul 12 '20 10:07 LayZeeDK

Since this issue was started, the testing guide was split into several pieces. The alert in question has moved to https://angular.io/guide/testing-components-scenarios#what-good-are-these-tests

LayZeeDK avatar Jul 17 '20 23:07 LayZeeDK

Since version 11.2.11, the following warning appeared when using RouterTestingModule :

'DEPRECATED: DI is instantiating a token "MockLocationStrategy" that inherits its @Injectable decorator but does not provide one itself.
This will become an error in a future version of Angular. Please add @Injectable() to the "MockLocationStrategy" class.'

Due to the lack of documentation, impossible to know if I'm doing wrong of if it's an angular issue

@Component({ template: '' })
class StubComponent {}

describe('MainMenuComponent', () => {
  let component: MainMenuComponent;
  let fixture: ComponentFixture<MainMenuComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [MainMenuComponent, StubComponent],
      imports: [
        RouterTestingModule.withRoutes([
          { path: '', component: StubComponent },
        ]),
      ],
    }).compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(MainMenuComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

Mikastark avatar Apr 27 '21 14:04 Mikastark

'DEPRECATED: DI is instantiating a token "MockLocationStrategy" that inherits its @Injectable decorator but does not provide one itself. This will become an error in a future version of Angular. Please add @Injectable() to the "MockLocationStrategy" class.'

Same here @MikaStark while runing tests.

guilhermebentomarques avatar May 26 '21 09:05 guilhermebentomarques

same here after upgrade to angular 12 ;)

albert-asin avatar Jun 04 '21 10:06 albert-asin

Also same here after Angular 12 upgrade. Any update on this?

condericson avatar Jun 08 '21 13:06 condericson

Hi all! I'm tracking this issue and am working with the product team to see when we can address this in the documentation. I appreciate your patience!

aikithoughts avatar Jun 08 '21 14:06 aikithoughts

If anyone lands here and needs an example to follow, this helped: https://stackoverflow.com/questions/53823091/unable-to-test-routing-using-routertestingmodule

Essentially, we need to add the following because DI no longer instantiates it `import {MockLocationStrategy} from '@angular/common/testing' import { LocationStrategy } from '@angular/common';

{ provide: LocationStrategy, useClass: MockLocationStrategy }`

benpetersen avatar Jun 18 '21 21:06 benpetersen

Essentially, we need to add the following because DI no longer instantiates it `import {MockLocationStrategy} from '@angular/common/testing' import { LocationStrategy } from '@angular/common';

{ provide: LocationStrategy, useClass: MockLocationStrategy }`

Is it a normal behavior intended by Angular or an Issue ?

Mikastark avatar Jun 21 '21 08:06 Mikastark

Updating angular/cli to 12.1.0 solved the WARN: 'DEPRECATED: DI is instantiating a token "MockLocationStrategy" that inherits its @Injectable decorator but does not provide one itself. This will become an error in a future version of Angular. Please add @Injectable() to the "MockLocationStrategy" class.'

toureholder avatar Jun 29 '21 14:06 toureholder

Funnily enough, the warning is indeed gone in 12.1.0, but back again in 12.1.1 🤔

Dinosys avatar Jul 07 '21 12:07 Dinosys

This issue seems to be solved. If someone get it again above version 12.1 you should remove your node_modules and package-lock.json and install again. It worked for me.

Mikastark avatar Aug 05 '21 16:08 Mikastark

Also happens in 12.2.0. Regardless of documentation (thank you @aikidave though).

As a first step - after having this issue for >1.5years - it would be very welcomed to have an angular-team/product-team approved way to fixing this here. Is it our error and should we do something about it, or is it yours?

{ provide: LocationStrategy, useClass: MockLocationStrategy }

Is adding this the recommended way of fixing this?

Thank you.

daniel-seitz avatar Aug 09 '21 09:08 daniel-seitz

Can anybody reproduce this? Trying hard with a new project with no luck so far..

daniel-seitz avatar Aug 10 '21 06:08 daniel-seitz

Reporting that using ~12.2.14 this issue is solvable by nuking node_modules.

yuri1969 avatar Feb 11 '22 07:02 yuri1969

We could rewrite parts of the testing guide to include RouterTestingHarness.

LayZeeDK avatar Feb 09 '23 07:02 LayZeeDK

The alert box described in this issue is still present at https://angular.io/guide/testing-components-scenarios#what-good-are-these-tests.

LayZeeDK avatar Feb 09 '23 07:02 LayZeeDK

This is not the case anymore.

Also we're looking at deprecating angular.io in favor of https://angular.dev/. Feel free to drop any suggestions for the new doc site, thank you.

JeanMeche avatar Feb 01 '24 20:02 JeanMeche

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.