-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Description
Describe the bug
The toContainElement assertion implementation allows for a Locator or an element to be passed:
vitest/packages/browser/src/client/tester/expect/toContainElement.ts
Lines 20 to 24 in 06d6207
| export default function toContainElement( | |
| this: MatcherState, | |
| actual: Element | Locator, | |
| expectedElement: Element | Locator | null, | |
| ): ExpectationResult { |
However, the TypeScript types only support passing an element:
vitest/packages/browser/jest-dom.d.ts
Line 210 in 06d6207
| toContainElement(element: HTMLElement | SVGElement | null): R |
The documentation also claims to only support elements:
vitest/docs/guide/browser/assertion-api.md
Line 327 in 06d6207
| 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.2Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.