Full answer
Testing strategy is about the return on each test. A common model is a pyramid:
- Unit tests (many, fast): pure functions, validators, pipes, reducers, services with simple dependencies. No TestBed needed for pure code. They run in milliseconds and pinpoint failures.
- Component tests (a good number):
TestBedwith the real template. Check what the user sees and does: render the component, set inputs withfixture.componentRef.setInput, click, and assert the DOM. Use component harnesses (HarnessLoader) for Material or your own components, because they hide DOM details and survive refactors. Stub child components or services that call the network. UseprovideHttpClientTestingwithHttpTestingControllerandverify()to check requests. - Integration tests (some): several pieces together, for example a routed feature with real guards, a store, and a mocked backend. They find wiring errors that unit tests miss.
RouterTestingHarnessnavigates and returns the component. - End-to-end tests (few): Playwright or Cypress in a real browser against a running app. Cover the money paths (login, enrol, pay). They are slow and flaky if overused, so keep them for high value flows.
Principles:
- Test behaviour, not implementation. Do not assert on private methods or the exact number of calls unless that is the behaviour.
- Prefer real objects for cheap collaborators and fakes for slow ones (network, time, storage). Avoid deep mocks that repeat the implementation.
- Control time:
fakeAsync, orvi.useFakeTimers/jasmine.clock. - Signals tests: set inputs, call
fixture.detectChanges()orawait fixture.whenStable(), and useTestBed.tick()for effects. - Make tests deterministic: no real network, fixed dates, isolated state per test (TestBed resets between tests).
- Keep a fast feedback loop: run affected tests on commit, the full suite plus e2e in CI, and track flaky tests.
- Coverage is a signal, not a goal. 100 percent coverage with no assertions has no value.
- Accessibility checks with axe in component or e2e tests catch many problems early.
For a school app: a validator for marks is a unit test, the marks form is a component test, "teacher enters marks and a student sees them" is an integration test with a mocked API, and "log in and download report card" is the end-to-end test.
typescript
import { Component, input } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
@Component({
selector: 'app-grade-badge',
template: `<span class="badge">{{ label() }}</span>`,
})
export class GradeBadgeComponent {
readonly marks = input.required<number>();
label(): string {
return this.marks() >= 40 ? 'Pass' : 'Fail';
}
}
export async function renderBadge(marks: number): Promise<string> {
await TestBed.configureTestingModule({ imports: [GradeBadgeComponent] }).compileComponents();
const fixture: ComponentFixture<GradeBadgeComponent> = TestBed.createComponent(GradeBadgeComponent);
fixture.componentRef.setInput('marks', marks);
fixture.detectChanges();
await fixture.whenStable();
return (fixture.nativeElement as HTMLElement).querySelector('.badge')?.textContent ?? '';
}
// it('shows Pass for 40 marks', async () => expect(await renderBadge(40)).toBe('Pass'));Angular 20.3. A Karma test runs this component test as written: a grade badge component with an input is rendered, and the DOM text is asserted.
Key points
- Many unit tests, a good number of component tests, some integration tests, few end-to-end tests.
- Component tests check DOM behaviour. Harnesses make them stable.
- Fake slow or random dependencies, control time, and keep tests deterministic.
- Coverage is a signal, not a goal.
Common mistakes
- Testing private methods and implementation details.
- Mocking everything so the test repeats the code.
- Covering every case with slow end-to-end tests.
Follow-ups
- When would you use a component harness?
- How do you test code that uses `setTimeout` or debounce?
- How do you reduce flaky end-to-end tests?
Tips
- Use the pyramid and justify it with speed and cost.
- Give an example from a school app.
- Say you test behaviour and use harnesses.
Evaluation rubric
- Describes the levels and their trade-offs.
- Applies good practices for mocking and time.
- Chooses tests by value.