Ty Kayn
Ty Kayn

Reputation: 809

how to avoid the dependencies hell with unit test in angular 2+

i see lots of examples about how to unit test simple components in angular 2+, but when it comes to test components who use services, it becomes a nightmare to maintain the test bed providers and imports. how can i avoid it ?

for example i have myComponents, who uses myService, who uses HttpClient. To test myComponent i must setup the providers for myService and HttpClient. If i add an other service to the constructor of myService, i will have to edit ALL the testbeds of the cmoponents who use this service.

can't i tell the testbed to fetch the default dependencies for these modules?

Upvotes: 3

Views: 2308

Answers (1)

Estus Flask
Estus Flask

Reputation: 222578

The problem results from wrong testing methodology. Unit testing is about testing single units.

In this scenario

myComponents, who uses myService, who uses HttpClient. To test myComponent i must setup the providers for myService and HttpClient.

it is myComponent unit that it tested. This means that any other unit should be mocked or stubbed, including myService.

While this

fetch the default dependencies for these modules

is considered not unit but integration/e2e test.

Upvotes: 1

Related Questions