Angular & Signals (283 Qs)•Part 10: Reactive Forms, Standalone Components, Signals & NgRx (256 - 283)
What is Component Test Harnesses?
A component harness is a testing API around an Angular directive or component to make tests simpler by hiding implementation details from test suites. This can be shared between unit tests, integration tests, and end-to-end tests. The idea for component harnesses comes from the PageObject pattern commonly used for integration testing.
Practical Code Example
typescript
// Component Test Harnesses:
import { MatButtonHarness } from '@angular/material/button/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
const loader = TestbedHarnessEnvironment.loader(fixture);
const buttonHarness = await loader.getHarness(MatButtonHarness);
await buttonHarness.click(); // Interacts with button cleanly without CSS selector coupling!