angular/components · error · Error

No active link could be found.

Error message

No active link could be found.

What it means

MatTabNavHarness.getActiveLink scans all links in the nav bar and returns the first whose isActive() is true; if none is active it throws 'No active link could be found.'. The harness relies on the nav bar's own active-link tracking (updateActiveLink), so the underlying link must actually be marked active.

Source

Thrown at src/material/tabs/testing/tab-nav-bar-harness.ts:58

  /**
   * Gets the list of links in the nav bar.
   * @param filter Optionally filters which links are included.
   */
  async getLinks(filter: TabLinkHarnessFilters = {}): Promise<MatTabLinkHarness[]> {
    return this.locatorForAll(MatTabLinkHarness.with(filter))();
  }

  /** Gets the active link in the nav bar. */
  async getActiveLink(): Promise<MatTabLinkHarness> {
    const links = await this.getLinks();
    const isActive = await parallel(() => links.map(t => t.isActive()));
    for (let i = 0; i < links.length; i++) {
      if (isActive[i]) {
        return links[i];
      }
    }
    throw new Error('No active link could be found.');
  }

  /**
   * Clicks a link inside the nav bar.
   * @param filter An optional filter to apply to the child link. The first link matching the filter
   *     will be clicked.
   */
  async clickLink(filter: TabLinkHarnessFilters = {}): Promise<void> {
    const tabs = await this.getLinks(filter);
    if (!tabs.length) {
      throw Error(`Cannot find mat-tab-link matching filter ${JSON.stringify(filter)}`);
    }
    await tabs[0].click();
  }

  /** Gets the panel associated with the nav bar. */
  async getPanel(): Promise<MatTabNavPanelHarness> {
    const link = await this.getActiveLink();

View on GitHub (pinned to 0411926e7d)

Solutions

  1. Ensure one link is active (activate via router navigation or set the active state) before calling getActiveLink()
  2. If links use routerLink, verify the test's current route matches one of them
  3. Await router navigation / whenStable before asserting
  4. For non-router links, call the nav bar's updateActiveLink() with the intended link

Example fix

// before
const active = await navHarness.getActiveLink(); // throws when none active

// after
await router.navigate(['/first']);
await fixture.whenStable();
const active = await navHarness.getActiveLink();
Defensive patterns

Strategy: validation

Validate before calling

const links = await navHarness.getLinks();
const anyActive = (await parallel(() => links.map(l => l.isActive()))).some(Boolean);
if (!anyActive) await router.navigate(['/first']);

Try / catch

try {
  const active = await navHarness.getActiveLink();
} catch (e) {
  if (String(e?.message).includes('No active link')) {
    await router.navigate(['/first']);
    await fixture.whenStable();
    active = await navHarness.getActiveLink();
  }
}

Prevention

When it happens

Trigger: Calling getActiveLink() when no MatTabLink has the active state (e.g. no router link matched, or the app never called updateActiveLink / never activated a link).

Common situations: Router-outlet based nav bars where the current URL matches no mat-tab-link routerLink; forgetting to set an initial active link in tests; using mat-tab-link without routerLink so isActive stays false.

Related errors


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