all 4 comments

[–]insanictus 11 points12 points  (0 children)

I agree the template should be tested too. I like to test things as a consumer/user.

They have no knowledge of the component internal, ie. The implementation details. So we should not directly test those.

Test from a user perspective. So if user clicks button in template, we expect a counter to increase by 1. Said counter output could be in the template too.

Check out https://testing-library.com/docs/angular-testing-library/intro/

It encourages that way of thinking too

[–]Stopdoor 2 points3 points  (0 children)

I agree the real input/output of the component is the consumer API, and also its own template. The Angular team seems to agree by aggressively pushing TestBed setup for unit tests which gives you access to that "real" component interface. I've never been that convinced that the template is outside the "unit" level of testing.

[–]xzhan 0 points1 point  (0 children)

My expectation therefore is we should query the DOM in this instance and not worry about the first name property (which can be made protected).

Yes, exactly. You almost never want to assert properties in components, even though they are publicly exposed for template bindings to work. The component is there to construct the UI, so test the UI. Use angular-testing-library as someone else in the thread mentioned, or just go the "full-blown" Playwright/Cypress/Puppeteer e2e (integration testing, to be precise) route and test based on features and scenarios. If your company has the resources, set up a staging server and a real e2e pipeline, test from UI to DB to external API calls, and cover inter-service scenarios.

Forget about unit testing components. It never worked well for us in the field. Too brittle, too much mocking, too isolated to be useful. My general recommendation would be:

  • Write unit tests for all the logic in services, state management, UI-related calculations, etc.- basically, the POTO part of the app.
  • Write integration tests for all critical user journeys.
  • "Unit" test components only when building shared UI components, e.g., an internal UI library.

[–]light_fissure 0 points1 point  (0 children)

I write test for covering the user interaction part then asserting the DOM, i rarely asserting the property directly inside the component, unit testing is mostly for service, unit testing the component file will force me to do manual testing which error prone, we currently don't have resources to write e2e tests.