angular/components · error · Error

No selected tab could be found.

Error message

No selected tab could be found.

What it means

MatTabGroupHarness.getSelectedTab iterates all tabs and returns the first whose isSelected() is true. If no tab reports selected, it throws 'No selected tab could be found.', indicating the harness found tabs but none was marked selected.

Source

Thrown at src/material/tabs/testing/tab-group-harness.ts:59

  /**
   * Gets the list of tabs in the tab group.
   * @param filter Optionally filters which tabs are included.
   */
  async getTabs(filter: TabHarnessFilters = {}): Promise<MatTabHarness[]> {
    return this.locatorForAll(MatTabHarness.with(filter))();
  }

  /** Gets the selected tab of the tab group. */
  async getSelectedTab(): Promise<MatTabHarness> {
    const tabs = await this.getTabs();
    const isSelected = await parallel(() => tabs.map(t => t.isSelected()));
    for (let i = 0; i < tabs.length; i++) {
      if (isSelected[i]) {
        return tabs[i];
      }
    }
    throw new Error('No selected tab could be found.');
  }

  /**
   * Selects a tab in this tab group.
   * @param filter An optional filter to apply to the child tabs. The first tab matching the filter
   *     will be selected.
   */
  async selectTab(filter: TabHarnessFilters = {}): Promise<void> {
    const tabs = await this.getTabs(filter);
    if (!tabs.length) {
      throw Error(`Cannot find mat-tab matching filter ${JSON.stringify(filter)}`);
    }
    await tabs[0].select();
  }
}

View on GitHub (pinned to 0411926e7d)

Solutions

  1. Ensure a tab is selected before calling getSelectedTab (set selectedIndex input or call harness.selectTab first)
  2. Await Angular change detection / harness stability (use await harness.host() or fixture.detectChanges + whenStable) before the assertion
  3. Check that getTabs()/filter arguments are not unintentionally excluding the selected tab

Example fix

// before
const selected = await harness.getSelectedTab(); // throws if nothing selected

// after
await harness.selectTab({label: 'First'});
const selected = await harness.getSelectedTab();
Defensive patterns

Strategy: validation

Validate before calling

// Guard before calling
const tabs = await harness.getTabs();
const anySelected = (await parallel(() => tabs.map(t => t.isSelected()))).some(Boolean);
if (!anySelected) await harness.selectTab({label: 'First'});

Try / catch

try {
  const selected = await harness.getSelectedTab();
} catch (e) {
  if (String(e?.message).includes('No selected tab')) {
    await harness.selectTab({label: 'First'});
    selected = await harness.getSelectedTab();
  }
}

Prevention

When it happens

Trigger: Calling harness.getSelectedTab() on a MatTabGroupHarness when the tab group has no selected tab (e.g. no selectedIndex bound/defaulted, or selection removed programmatically before the harness call).

Common situations: Test setups where selectedIndex is undefined or the group renders tabs lazily before selection is initialized; race conditions where the test asserts selection before change detection marks the tab selected; custom grouping where `mat-tab-group` has `dynamicHeight`/no preselected index and the app never sets one.

Related errors


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