skills/dev-skills/angular-developer/references/component-harnesses.md
Component harnesses are the standard, preferred way to interact with components in tests. They provide a robust, user-centric API that makes tests less brittle and easier to read by insulating them from changes to a component's internal DOM structure.
button.click(), slider.getValue()) instead of through DOM queries (fixture.nativeElement.querySelector(...)).Angular Material provides a test harness for every component in its library.
The TestbedHarnessEnvironment is the entry point for using harnesses in unit tests.
MatButtonHarnessimport {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {MatButtonHarness} from '@angular/material/button/testing';
import {MyButtonContainerComponent} from './my-button-container.component';
describe('MyButtonContainerComponent', () => {
let fixture: ComponentFixture<MyButtonContainerComponent>;
let loader: HarnessLoader;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [MyButtonContainerComponent, MatButtonModule],
}).compileComponents();
fixture = TestBed.createComponent(MyButtonContainerComponent);
// Create a harness loader for the component's fixture
loader = TestbedHarnessEnvironment.loader(fixture);
});
it('should find a button with specific text', async () => {
// Load the harness for a MatButton with the text "Submit"
const submitButton = await loader.getHarness(MatButtonHarness.with({text: 'Submit'}));
// Use the harness API to interact with the component
expect(await submitButton.isDisabled()).toBe(false);
await submitButton.click();
// ... assertions
});
});
HarnessLoader: An object used to find and create harness instances. Get a loader for your component's fixture using TestbedHarnessEnvironment.loader(fixture).
loader.getHarness(HarnessClass): Asynchronously finds and returns a harness instance for the first matching component.
HarnessClass.with({ ... }): Many harnesses provide a static with method that returns a HarnessPredicate. This allows you to filter and find components based on their properties, like text, selector, or disabled state. Always use this to precisely target the component you want to test.
Harness API: Once you have a harness instance, use its methods (e.g., .click(), .getText(), .getValue()) to interact with the component. These methods automatically handle waiting for async operations and change detection.