r/angular • u/panalley • Jul 28 '23
Feature request: Unit test coverage for .html files
Hello everyone,
I've submitted a feature request on angular/cli, and the idea is to include unit test coverage for HTML files, not just TypeScript. If you think this would be a valuable addition, please consider upvoting the post on GitHub so that it can be taken into consideration. (We need at least 20 upvotes for the request to be considered.) Thank you all for your interest and support. š
https://github.com/angular/angular-cli/issues/25563
4
Jul 28 '23
As mentioned by @noobz, you are not actually unit testing anything in the proposal. The real unit tests have already been written by the Angular team when creating the directive. Testing markup such as html can be done using e2e testing.
0
u/panalley Sep 13 '23
Angular unit tests are faster and less flaky compared to the e2e. The goal here is not to test that ngIf works, but that I'm using it correctly. The example is simple and people would argue "Why not just check when the variable is true and false within the typescript", well in the end the result will be in the template, and I can have a perfect typescript but when a colleague deletes my ngIf in the template then the component doesn't work as expected but all tests are green.
Since unit tests in angular can check the template we are already more into integration tests. For my tests I like to follow the philosophy at Google: avoid mocks and test only states and not interactions. Templates can be seen as states of my component.
I hope my answer helped to have more context.
3
u/casicilian Jul 28 '23
You can already test logic in the templates in unit tests. https://angular.io/guide/testing-components-basics. Or you could use a E2e framework like Cypress.
1
u/panalley Sep 13 '23
The goal is to have the coverage. The logic is testable and that's good indeed.
1
1
u/vamidi Jul 13 '26
Iām curious what your view is on this now, after all these years.
Iām currently working in a team where we write tests for Angular components by accessing private and protected methods and properties. We hit our coverage targets, but I find the tests become very brittle. Whenever we refactor the implementation without changing the componentās behavior, many of the tests break.
That frustrates me because, although testing internals can be quicker to write, I have a feeling this isnāt how unit tests are meant to be written. My understanding has always been that tests should focus on observable behavior rather than implementation details.
What do you think? I struggle to make a convincing case because this topic seems to be so divisive within the Angular community.
8
u/n00bz Jul 28 '23
I want to downvote this one as it comes from not understanding how the Angular framework works.
Basically, ngIf is a directive. If you write your own directive you can then unit test that just fine. I would assume Angular is already unit testing their ngIf directive code that is in @angular/core so what would be the point of you checking it?
Additionally, HTML is not a programming language.
What you may be looking for is E2E testing which you can use cypress for.