Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 994
Ty Kayn Avatar asked Nov 18 '25 11:11

Ty Kayn


1 Answers

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.

like image 184
Estus Flask Avatar answered Nov 21 '25 02:11

Estus Flask



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!