Testing Angular private class members in practice

Karim Karimov
Practical Angular Karma testing
2 min readAug 23, 2019

In this tutorial, we will test the private property and method of AppComponent.

app.component.ts

Some testers say that private member should not be tested, only public API is enough. But in reality, private method logic and conditions can be very complex and their unit tests become also essential for verification. I agree the tester should not modify the original code, but on the other hand, providing testable code is developers’ duty.

Access level modifiers are recognized only by Typescript. After code compilation to the Javascript code, restrictions are gone. In this case, we can access private class members not directly but using brackets [].

Test case #1

To analyze in detail, the complete project source code is in Github.

Comments and suggestions are welcome.

--

--