{"record":{"id":"72e9d5d2fd56677b","repo":"angular/components","slug":"no-harness-was-located-at-index-offset","errorCode":null,"errorMessage":"No harness was located at index ${offset}","messagePattern":"No harness was located at index (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cdk/testing/harness-environment.ts","lineNumber":270,"sourceCode":"   * Searches for an instance of the component corresponding to the given harness type and index\n   * under the `HarnessEnvironment`'s root element, and returns a `ComponentHarness` for that\n   * instance. The index specifies the offset of the component to find. If no matching\n   * component is found at that index, an error is thrown.\n   * @param query A query for a harness to create\n   * @param index The zero-indexed offset of the component to find\n   * @return An instance of the given harness type\n   * @throws If a matching component instance can't be found.\n   */\n  async getHarnessAtIndex<T extends ComponentHarness>(\n    query: HarnessQuery<T>,\n    offset: number,\n  ): Promise<T> {\n    if (offset < 0) {\n      throw Error('Index must not be negative');\n    }\n    const harnesses = await this.locatorForAll(query)();\n    if (offset >= harnesses.length) {\n      throw Error(`No harness was located at index ${offset}`);\n    }\n    return harnesses[offset];\n  }\n\n  /**\n   * Searches for all instances of the component corresponding to the given harness type under the\n   * `HarnessEnvironment`'s root element, and returns a list `ComponentHarness` for each instance.\n   * @param query A query for a harness to create\n   * @return A list instances of the given harness type.\n   */\n  getAllHarnesses<T extends ComponentHarness>(query: HarnessQuery<T>): Promise<T[]> {\n    return this.locatorForAll(query)();\n  }\n\n  /**\n   * Searches for all instance of the component corresponding to the given harness type under the\n   * `HarnessEnvironment`'s root element, and returns the number that were found.\n   * @param query A query for a harness to create","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk/testing/harness-environment.ts#L252-L288","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the component under test actually renders at least offset+1 matching elements; await fixture.whenStable()/detectChanges first.","Use locatorForOptional/getAllHarnesses and assert array length before indexing.","Print harnesses.length to confirm how many matches exist and adjust the index."],"exampleFix":"// before\nconst harness = await env.getHarnessAtIndex(RowHarness, 2);\n// after\nconst rows = await env.getAllHarnesses(RowHarness);\nexpect(rows.length).toBeGreaterThan(2);\nconst harness = rows[2];","handlingStrategy":"validation","validationCode":"const all = await env.getAllHarnesses(MyHarness);\nif (offset >= all.length) throw new Error(`expected >= ${offset + 1} harnesses, found ${all.length}`);","typeGuard":"const inRange = <T>(arr: T[], i: number): i is number => i >= 0 && i < arr.length;","tryCatchPattern":"try { return await env.getHarnessAtIndex(MyHarness, i); } catch (e) { if ((e as Error).message.startsWith('No harness was located')) return undefined; throw e; }","preventionTips":["await fixture.whenStable() and detectChanges before querying","Assert rendered element counts in beforeEach","Use locatorForOptional/getAllHarnesses when existence is uncertain"],"tags":["angular-cdk","testing","component-harness","index-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}