Skip to content

Improve TestModuleMetadata typings #37178

@fvoska

Description

@fvoska

🚀 feature request

Relevant Package

This feature request is for @angular/core

Description

Typings for TestModuleMetadata are too loose and they do not provide proper type-checking. Ths can lead to hard-to-track bugs in tests in case there is a typo or some other error in TestBed configuration object.

Example issue:

TestBed.configureTestingModule({
  providers: [{
    provibe: SomeService,
    useClass: SomeServiceMock,
  }],
})

If SomeService is decorated as @Injectable({ providedIn: 'root' }), the test for the above TestBed configuration will actually create an instance of SomeService instead of SomeServiceMock because there is a typo (provibe instead of provide) in the provider so this provider is basically ignored. This kind of an error can be hard to pin down and could be easily caught by stricter typings for TestModuleMetadata.

Describe the solution you'd like

I suggest updating typings of TestModuleMetadata to match the typings of NgModule interface. Please check the related PR I created.

Describe alternatives you've considered

Alternative solution is to explicitly type the providers yourself:

const testProviders: Array<Provider> = [{
  provibe: SomeService, // now this typo will be caught
  useClass: SomeServiceMock,
}];

TestBed.configureTestingModule({
  providers: testProviders,
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testingIssues related to Angular testing features, such as TestBedcross-cutting: typesfeatureIssue that requests a new featurefeature: under considerationFeature request for which voting has completed and the request is now under consideration

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions