{"record":{"id":"007649a2a4033204","repo":"angular/components","slug":"unable-to-retrieve-option-groups-for-autocomplete","errorCode":null,"errorMessage":"Unable to retrieve option groups for autocomplete. Autocomplete panel is closed.","messagePattern":"Unable to retrieve option groups for autocomplete\\. Autocomplete panel is closed\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/material/autocomplete/testing/autocomplete-harness.ts","lineNumber":104,"sourceCode":"  async getOptions(filters?: Omit<OptionHarnessFilters, 'ancestor'>): Promise<MatOptionHarness[]> {\n    if (!(await this.isOpen())) {\n      throw new Error('Unable to retrieve options for autocomplete. Autocomplete panel is closed.');\n    }\n\n    return this._documentRootLocator.locatorForAll(\n      MatOptionHarness.with({\n        ...(filters || {}),\n        ancestor: await this._getPanelSelector(),\n      } as OptionHarnessFilters),\n    )();\n  }\n\n  /** Gets the option groups inside the autocomplete panel. */\n  async getOptionGroups(\n    filters?: Omit<OptgroupHarnessFilters, 'ancestor'>,\n  ): Promise<MatOptgroupHarness[]> {\n    if (!(await this.isOpen())) {\n      throw new Error(\n        'Unable to retrieve option groups for autocomplete. Autocomplete panel is closed.',\n      );\n    }\n\n    return this._documentRootLocator.locatorForAll(\n      MatOptgroupHarness.with({\n        ...(filters || {}),\n        ancestor: await this._getPanelSelector(),\n      } as OptgroupHarnessFilters),\n    )();\n  }\n\n  /** Selects the first option matching the given filters. */\n  async selectOption(filters: OptionHarnessFilters): Promise<void> {\n    await this.focus(); // Focus the input to make sure the autocomplete panel is shown.\n    const options = await this.getOptions(filters);\n    if (!options.length) {\n      throw Error(`Could not find a mat-option matching ${JSON.stringify(filters)}`);","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/material/autocomplete/testing/autocomplete-harness.ts#L86-L122","documentation":"MatAutocompleteHarness.getOptionGroups() locates MatOptgroupHarness instances inside the autocomplete panel, which only exists while the panel is open. The harness throws this error when isOpen() is false instead of returning an empty array.","triggerScenarios":"Calling getOptionGroups() while the autocomplete panel is closed — i.e. the trigger was never focused, or the panel closed due to blur/Escape/option selection before the call.","commonSituations":"Testing grouped options without opening the panel first; forgetting that selecting an option closes the panel before further group queries; race conditions where the panel hasn't rendered yet.","solutions":["Open the autocomplete first (focus the input / type to trigger filtering) and verify isOpen() is true before calling getOptionGroups()","Await the panel rendering (tick/waitFor) after simulating input","Re-open the panel if a previous action (e.g. selecting an option) closed it"],"exampleFix":"// before\nconst groups = await harness.getOptionGroups(); // throws: panel closed\n// after\nawait harness.focus();\nawait harness.enterText('q');\nconst groups = await harness.getOptionGroups();","handlingStrategy":"validation","validationCode":"if (await harness.isOpen()) {\n  const groups = await harness.getOptionGroups();\n}","typeGuard":null,"tryCatchPattern":"try {\n  groups = await harness.getOptionGroups();\n} catch (e) {\n  if ((e as Error).message.includes('panel is closed')) {\n    await harness.focus();\n    groups = await harness.getOptionGroups();\n  } else { throw e; }\n}","preventionTips":["Open the panel and confirm isOpen() before querying groups","Handle the panel closing after each option selection in test flows","Use fakeAsync/tick or waitFor to ensure the panel has rendered"],"tags":["angular","material","testing","component-harness","autocomplete","state-error"],"backgroundTag":"panel-closed","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}