angular/components · error

No harness was located at index ${offset}

Error message

No harness was located at index ${offset}

What it means

Thrown by getHarnessAtIndex when the resolved index is beyond the end of the matched harness array. After locating all harnesses for the query, none exists at the requested offset, so the method refuses to return undefined.

Source

Thrown at src/cdk/testing/harness-environment.ts:270

   * Searches for an instance of the component corresponding to the given harness type and index
   * under the `HarnessEnvironment`'s root element, and returns a `ComponentHarness` for that
   * instance. The index specifies the offset of the component to find. If no matching
   * component is found at that index, an error is thrown.
   * @param query A query for a harness to create
   * @param index The zero-indexed offset of the component to find
   * @return An instance of the given harness type
   * @throws If a matching component instance can't be found.
   */
  async getHarnessAtIndex<T extends ComponentHarness>(
    query: HarnessQuery<T>,
    offset: number,
  ): Promise<T> {
    if (offset < 0) {
      throw Error('Index must not be negative');
    }
    const harnesses = await this.locatorForAll(query)();
    if (offset >= harnesses.length) {
      throw Error(`No harness was located at index ${offset}`);
    }
    return harnesses[offset];
  }

  /**
   * Searches for all instances of the component corresponding to the given harness type under the
   * `HarnessEnvironment`'s root element, and returns a list `ComponentHarness` for each instance.
   * @param query A query for a harness to create
   * @return A list instances of the given harness type.
   */
  getAllHarnesses<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T[]> {
    return this.locatorForAll(query)();
  }

  /**
   * Searches for all instance of the component corresponding to the given harness type under the
   * `HarnessEnvironment`'s root element, and returns the number that were found.
   * @param query A query for a harness to create

View on GitHub (pinned to 0411926e7d)

Solutions

  1. Verify the component under test actually renders at least offset+1 matching elements; await fixture.whenStable()/detectChanges first.
  2. Use locatorForOptional/getAllHarnesses and assert array length before indexing.
  3. Print harnesses.length to confirm how many matches exist and adjust the index.

Example fix

// before
const harness = await env.getHarnessAtIndex(RowHarness, 2);
// after
const rows = await env.getAllHarnesses(RowHarness);
expect(rows.length).toBeGreaterThan(2);
const harness = rows[2];
Defensive patterns

Strategy: validation

Validate before calling

const all = await env.getAllHarnesses(MyHarness);
if (offset >= all.length) throw new Error(`expected >= ${offset + 1} harnesses, found ${all.length}`);

Type guard

const inRange = <T>(arr: T[], i: number): i is number => i >= 0 && i < arr.length;

Try / catch

try { return await env.getHarnessAtIndex(MyHarness, i); } catch (e) { if ((e as Error).message.startsWith('No harness was located')) return undefined; throw e; }

Prevention

When it happens

Trigger: Calling getHarnessAtIndex(query, offset) where fewer harnesses match than offset+1 — e.g. querying for the 3rd mat-tab but only 2 tabs are rendered, or before async rendering has completed.

Common situations: Assuming all expected components are rendered in the fixture, not awaiting asynchronous template rendering, dynamic lists with fewer items than expected, or forgetting change detection before querying.

Related errors


AI-assisted analysis of angular/components@0411926e7d (2026-08-31). Data as JSON: /api/errors/72e9d5d2fd56677b. Report an issue: GitHub.