Sub topic 12
What to test and what to leave out
This chapter assumes you can write a basic Vitest test with TestBed, query the DOM through a fixture, and replace a service with a mock provider. It covers how to choose which tests to write, not how to write them.
Test behavior, not implementation
A component test should describe something a user or a parent component can observe. That means three things:
- The rendered DOM, such as text, attributes, and which elements exist.
- The outputs the component emits.
- The calls it makes to its dependencies, such as "requests the user list once".
Anything else is an implementation detail. Examples are the name of a private method, whether state lives in a signal or a plain field, and the order of internal calls. Tests that depend on these details break when you refactor, even though the component still works.
Consider CounterComponent from earlier chapters. Its count signal is protected. A test that reaches in with fixture.componentInstance['count']() skips the type check and ties the test to a field name. A better test clicks the button and reads what the user sees:
import { TestBed } from '@angular/core/testing';
import { describe, it, expect } from 'vitest';
import { CounterComponent } from './counter.component';
describe('CounterComponent behavior', () => {
it('accumulates the default step across clicks', () => {
const fixture = TestBed.createComponent(CounterComponent);
fixture.detectChanges();
const button: HTMLButtonElement = fixture.nativeElement.querySelector('button');
button.click();
button.click();
fixture.detectChanges();
const text = fixture.nativeElement.querySelector('[data-testid="count"]').textContent;
expect(text).toContain('Count: 2');
});
});You could change count to a different signal type or rename increment(), and this test would still pass.
A decision guide
Ask what could go wrong in the code you are about to test, and who would notice.
| Worth a test | Usually not worth a test |
|---|---|
| Conditional rendering (`@if`, `@for` with empty and full data) | Static template text with no logic |
| Output emission and its payload | That Angular's `input()` stores a value |
| Calls to services, including arguments | Private helper methods directly |
| Error and loading states | Third-party library internals |
| Branches with real logic (validation, formatting) | Exact CSS classes used only for styling |
Avoid testing the framework. If you test that clicking a button with (click) calls a handler, you are mostly checking that Angular binds events. Check the effect of the handler instead.
Common mistakes
- Snapshot-style assertions on whole templates. They fail on every markup change and teach people to update snapshots without reading them. Assert on the specific text or element that matters.
- Mocking everything. If you mock every collaborator, the test only proves your mocks agree with each other. Mock at the boundary, such as HTTP and services with side effects. Keep pure helpers real.
- One huge test per component. A failure then tells you little. Write one
itper behavior, with a name that reads as a sentence. - Chasing a coverage number. High coverage shows which lines ran, not that the behavior was checked. A test with no
expectcan still raise coverage.
Choosing the level
Test pure logic, such as formatting or validation, as plain functions with no TestBed. Test template behavior through the component. Test HTTP through HttpTestingController on the service. Each layer gets the cheapest test that can fail for the right reason.
Exercise
A teammate adds a test that calls a private formatLabel() method on a component and checks its return value. The method is only used to build text shown in the template. What would you change, and why?
Show answer
Test the rendered text instead. Create the component, set the needed inputs, run detectChanges, and assert on the DOM text. The private method is an implementation detail, so the test would break if it were renamed or inlined even though the visible behavior is unchanged. If the formatting logic is complex enough to need many cases, move it into an exported pure function and test that function directly.