Skip to content

toContainElement types don't allow locators #8910

@billyjanitsch

Description

@billyjanitsch

Describe the bug

The toContainElement assertion implementation allows for a Locator or an element to be passed:

export default function toContainElement(
this: MatcherState,
actual: Element | Locator,
expectedElement: Element | Locator | null,
): ExpectationResult {

However, the TypeScript types only support passing an element:

toContainElement(element: HTMLElement | SVGElement | null): R

The documentation also claims to only support elements:

function toContainElement(element: HTMLElement | SVGElement | null): Promise<void>

As a result, the following code executes correctly but fails to type check:

const parent = page.getByTestId("parent");
const child = page.getByTestId("child");
await expect.element(parent).toContainElement(child);

Can the types and docs please be updated to match the implementation?

Reproduction

I couldn't figure out how to get browser tests working on StackBlitz. Below is a minimal reproduction; let me know if you need it turned into a repo.

import { expect, test } from "vitest";
import { page } from "vitest/browser";
import { render } from "vitest-browser-react";

test("parent contains child", async () => {
  await render(
    <div data-testid="parent">
      <div data-testid="child" />
    </div>,
  );
  const parent = page.getByTestId("parent");
  const child = page.getByTestId("child");
  await expect.element(parent).toContainElement(child);
});

System Info

System:
    OS: macOS 26.0.1
    CPU: (10) arm64 Apple M4
    Memory: 802.73 MB / 16.00 GB
    Shell: 4.1.2 - /opt/homebrew/bin/fish
  Binaries:
    Node: 24.11.0 - /Users/billy/.local/share/nvm/v24.11.0/bin/node
    npm: 11.6.2 - /Users/billy/.local/share/nvm/v24.11.0/bin/npm
  Browsers:
    Chrome: 141.0.7390.123
    Firefox: 136.0.2
    Safari: 26.0.1
  npmPackages:
    playwright: ^1.56.1 => 1.56.1
    vitest: ^4.0.6 => 4.0.6
    vitest-browser-react: ^2.0.2 => 2.0.2

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)pr welcome

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions