facebook/react · error · Error

Expected 1 but found ${all.length} instances ${message}

Error message

Expected 1 but found ${all.length} instances ${message}

What it means

The singular query helpers on ReactTestInstance (find, findByType, findByProps) require exactly one match; expectOne throws 'Expected 1 but found N instances ...' when the predicate matches several rendered instances, listing the query in the message suffix.

Source

Thrown at packages/react-test-renderer/src/ReactTestRenderer.js:456

  });

  return results;
}

function expectOne(
  all: Array<ReactTestInstance>,
  message: string,
): ReactTestInstance {
  if (all.length === 1) {
    return all[0];
  }

  const prefix =
    all.length === 0
      ? 'No instances found '
      : `Expected 1 but found ${all.length} instances `;

  throw new Error(prefix + message);
}

function propsMatch(props: Object, filter: Object): boolean {
  for (const key in filter) {
    if (props[key] !== filter[key]) {
      return false;
    }
  }
  return true;
}

function create(
  element: React$Element<any>,
  options: TestRendererOptions,
): {
  _Scheduler: typeof Scheduler,
  root: void,
  toJSON(): Array<ReactTestRendererNode> | ReactTestRendererNode | null,

View on GitHub (pinned to eafeac097b)

Solutions

  1. Narrow the query: add distinguishing props and use findByProps({type: X, ...}) instead of findByType
  2. Switch to findAllByType(Component) and pick the specific index you mean, asserting the expected count
  3. Query through a parent instance (parent.findByType) to scope the search to one subtree

Example fix

// before
const input = root.findByType('input'); // throws: Expected 1 but found 3 instances

// after
const inputs = root.findAllByType('input');
expect(inputs).toHaveLength(3);
const email = root.findByProps({type: 'email'});
Defensive patterns

Strategy: validation

Validate before calling

// Assert cardinality explicitly, then pick the instance you mean
const buttons = root.findAllByType(Button);
assert.equal(buttons.length, 3, `expected 3 Buttons, got ${buttons.length}`);
const primary = root.findByProps({variant: 'primary'});

Prevention

When it happens

Trigger: Calling findByType(Component)/findByProps({...}) when the tree contains two or more matching instances, such as a component rendered in a list or in both a desktop and mobile branch.

Common situations: A list renders the same child type multiple times; a component appears in both a header and body slot; props filter is too loose (e.g. matching on a className shared by many nodes).

Related errors


AI-assisted analysis of facebook/react@eafeac097b (2026-08-21). Data as JSON: /api/errors/5dc668919baaedae. Report an issue: GitHub.