jest-dom/prefer-pressed

July 31, 2026 ยท View on GitHub

๐Ÿ“ Prefer toBePressed over checking attributes.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

Rule Details

This rule aims to prevent false positives and improve readability and should only be used with the @testing-library/jest-dom package. The rule is autofixable and will replace any instances of .toHaveProperty() or .toHaveAttribute() with .toBePressed(), .toBePartiallyPressed(), and not.toBePressed() as appropriate.

Examples of incorrect code for this rule:

expect(element).toHaveProperty("aria-pressed", true);
expect(element).toHaveAttribute("aria-pressed", false);
expect(element).toHaveProperty("aria-pressed", something);

expect(element).toHaveAttribute("aria-pressed");
expect(element).not.toHaveProperty("aria-pressed");

expect(element).toHaveAttribute("aria-pressed", "mixed");

expect(element).not.not.toBePressed();

Examples of correct code for this rule:

expect(element).toBePressed();

expect(element).not.toBePressed();

expect(element).toHaveAttribute("aria-label");

expect(element).toBePartiallyPressed();

When Not To Use It

Don't use this rule if you:

  • don't use jest-dom
  • want to allow .toHaveAttribute() and .toHaveProperty() for aria-pressed

Further Reading