Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk4 min

Using Component Harnesses in Angular Tests

Use Angular component harnesses to test shared, interactive components through stable APIs rather than private DOM details. Learn TestBed setup, loader scope, async behavior, and custom harness design.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Angular component harnesses let tests interact with components through supported, user-oriented APIs instead of depending on private DOM structure. They are most useful for shared, interactive components whose markup may change independently of the tests that use them.

What a component harness does

A component harness is a class that gives tests a stable API for operating a component in ways that resemble user interaction. Rather than selecting internal elements and dispatching DOM events directly, a test can call methods such as toggle() or read state through isOpen(). That keeps assertions focused on behavior and reduces reliance on markup and CSS details.

Angular describes harnesses as reusable across unit and end-to-end tests. The harness API separates tests that consume a component from the component’s internal implementation.

Use a harness in a TestBed test

The harness infrastructure is part of Angular CDK. If the project does not already include it, add the package with ng add @angular/cdk. In a TestBed test, create the fixture, make a loader scoped to it, and ask that loader for the component’s harness:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const fixture = TestBed.createComponent(MyComponent);
const loader = TestbedHarnessEnvironment.loader(fixture);
const component = await loader.getHarness(MyComponentHarness);

Harness APIs are generally asynchronous, so await calls that query or interact with harnesses. The current Angular guide documents TestBed unit tests and Selenium WebDriver end-to-end tests as built-in CDK environments; support can vary by Angular/CDK version, so check the documentation matching the versions in your project. See Angular’s guide to using component harnesses.

Choose the loader that contains the element

Loader Search scope Use it for
TestbedHarnessEnvironment.loader(fixture) The fixture root Elements rendered within the tested component fixture.
TestbedHarnessEnvironment.documentRootLoader(fixture) The document root Content rendered outside the fixture, such as dialogs or CDK overlays attached under document.body.

If a harness query cannot find an overlay, the issue may be its search scope rather than the component: use the document-root loader when the target is outside the fixture. Angular also provides harnessForFixture as an option for loading one harness directly for a fixture root. The API details are in the TestbedHarnessEnvironment reference.

Query one or more harnesses

A HarnessLoader can retrieve one matching harness, enumerate matches, or test for their presence. Use the query that matches the assertion rather than fetching elements and inspecting private markup.

  • getHarness retrieves one matching harness.
  • getAllHarnesses retrieves all matching harnesses.
  • getHarnessAtIndex retrieves a match by index.
  • countHarnesses counts matches.
  • hasHarness checks whether a match exists.

Many harness classes provide a static with() helper that creates a HarnessPredicate. Predicates support meaningful filters, such as a selector or component-specific text, so a test can identify the intended instance without relying on its private DOM structure. See the HarnessLoader API and HarnessPredicate API.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Handle promises and change detection

Await harness calls consistently. TestBed harnesses run change detection before reading element state and after interactions, which is generally suitable for ordinary tests. When a test needs to examine an intermediate state while asynchronous work is still pending, use manualChangeDetection to take control of change detection for that block. Angular documents these behaviors in its harness testing guide.

Write a custom harness around user-visible behavior

Extend ComponentHarness and define a static hostSelector that usually matches the component or directive selector. Expose useful user actions and observable state—not every internal element. For example, a menu harness might offer an action to open the menu and a method to report whether it is open.

Use locatorFor, locatorForOptional, and locatorForAll for element queries. These locators resolve against the current DOM, which helps when conditional content is removed and later recreated. Interact through TestElement rather than direct DOM access; it is designed to work across environments. The Angular guide to creating component harnesses covers the authoring API.

For components with multiple instances, add a static with() method that builds a HarnessPredicate for common selectors or component-specific filters. Tests can then select the instance they mean through the harness API.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Decide whether a component needs a harness

Harnesses add an abstraction, so they are not necessary for every component. Angular recommends them particularly for shared components used in many places that also have user interaction. A one-off page often gains less: its implementation and tests tend to change together. A custom harness can still be worthwhile when the same component needs a consistent test API in unit and end-to-end tests.

Support for other testing environments

Beyond the built-in TestBed and Selenium WebDriver environments named in Angular’s guide, using harnesses requires environment-specific work. A custom environment needs a TestElement implementation for its raw element type and a concrete HarnessEnvironment subclass. It must locate matching raw elements, create test elements and child environments, identify the document root, stabilize Angular work, wait for tasks outside Angular, and expose a loader factory to test authors. Consult the environment-authoring guidance and verify the support available in the Angular/CDK version your project uses.

The Angular documentation checked on October 5, 2026, reported version v22.2.1+sha-ef03596. APIs and supported environments may change; use documentation aligned with your installed Angular and CDK versions.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.