{"record":{"id":"c3143f53cf1722cf","repo":"angular/components","slug":"could-not-find-item-matching-json-stringify-item","errorCode":null,"errorMessage":"Could not find item matching ${JSON.stringify(itemFilter)}","messagePattern":"Could not find item matching (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/material/menu/testing/menu-harness.ts","lineNumber":221,"sourceCode":"    return (await this.host()).matchesSelector(MatMenuHarness.hostSelector);\n  }\n\n  /** Gets the submenu associated with this menu item, or null if none. */\n  async getSubmenu(): Promise<MatMenuHarness | null> {\n    if (await this.hasSubmenu()) {\n      return new MatMenuHarness(this.locatorFactory);\n    }\n    return null;\n  }\n}\n\nexport async function clickItemImplementation(\n  items: MatMenuItemHarness[],\n  itemFilter: Omit<MenuItemHarnessFilters, 'ancestor'>,\n  subItemFilters: Omit<MenuItemHarnessFilters, 'ancestor'>[],\n): Promise<void> {\n  if (!items.length) {\n    throw Error(`Could not find item matching ${JSON.stringify(itemFilter)}`);\n  }\n\n  if (!subItemFilters.length) {\n    return await items[0].click();\n  }\n\n  const menu = await items[0].getSubmenu();\n  if (!menu) {\n    throw Error(`Item matching ${JSON.stringify(itemFilter)} does not have a submenu`);\n  }\n  return menu.clickItem(...(subItemFilters as [Omit<MenuItemHarnessFilters, 'ancestor'>]));\n}\n","sourceCodeStart":203,"sourceCodeEnd":234,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/material/menu/testing/menu-harness.ts#L203-L234","documentation":"The MatMenu harness `clickItem` helper throws when the harness query for menu items returns no matches. It means no `MatMenuItemHarness` matching the supplied `itemFilter` (text, icon, etc., excluding `ancestor`) exists in the menu at click time. The library throws early instead of clicking undefined.","triggerScenarios":"Calling `await menuHarness.clickItem({text: 'Save'})` (or the exported `clickItemImplementation`) when no menu item has that text, the menu was never opened, or the filter (e.g. regex or icon) does not match any rendered item.","commonSituations":"Typos or case differences in item text; forgetting to call `open()`/hover on the trigger so items are not rendered; test data changed so the menu label no longer matches; using `text` with regex when plain text is expected.","solutions":["Open the menu (via trigger harness `click()`/`hover()`) before calling clickItem","Log `await menuHarness.getItems()` to see the actual items and fix the filter","Correct the `text`/`icon`/`selector` filter to match an existing item exactly"],"exampleFix":"// before\nawait menuHarness.clickItem({text: 'svae'});\n// after\nawait menuHarness.clickItem({text: 'Save'});","handlingStrategy":"validation","validationCode":"const items = await menuHarness.getItems();\nif (!items.length) {\n  throw new Error(`Menu empty before clickItem; check filters/menu open state`);\n}","typeGuard":"function hasItems<T>(arr: T[] | undefined | null): arr is T[] { return Array.isArray(arr) && arr.length > 0; }","tryCatchPattern":"try {\n  await menuHarness.clickItem({text: 'Save'});\n} catch (e) {\n  if (String(e?.message).startsWith('Could not find item matching')) {\n    fail('Menu item filter matched nothing: open the menu and verify item text');\n  }\n  throw e;\n}","preventionTips":["Always open the menu before querying/clicking items","Use getItems() to debug filters during test authoring","Keep menu item labels in constants shared with tests"],"tags":["angular","material","testing","harness"],"backgroundTag":"harness-element-not-found","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}