Skip to content

feat: add rules for toBePressed, toBePartiallyPressed, toBePartiallyChecked matchers#419

Open
shamrt-v wants to merge 8 commits intotesting-library:mainfrom
shamrt-v:feat/new-matcher-rules
Open

feat: add rules for toBePressed, toBePartiallyPressed, toBePartiallyChecked matchers#419
shamrt-v wants to merge 8 commits intotesting-library:mainfrom
shamrt-v:feat/new-matcher-rules

Conversation

@shamrt-v
Copy link
Contributor

@shamrt-v shamrt-v commented Feb 12, 2026

What:

Add three new ESLint rules for recent jest-dom matchers:

  • prefer-pressed — suggests toBePressed() / not.toBePressed() instead of manual aria-pressed attribute checks
  • prefer-partially-pressed — suggests toBePartiallyPressed() instead of toHaveAttribute('aria-pressed', 'mixed')
  • prefer-partially-checked — suggests toBePartiallyChecked() instead of toHaveAttribute('aria-checked', 'mixed')

All three rules are included in the all config but excluded from recommended to avoid breaking changes.

Why:

jest-dom v6.7.0+ added toBePressed and toBePartiallyPressed matchers, and toBePartiallyChecked has existed for years — but eslint-plugin-jest-dom had no rules to guide users toward them.

How:

  • prefer-pressed reuses the existing createBannedAttributeRule factory (same pattern as prefer-checked)
  • Created a new createPartiallyBannedAttributeRule factory for the two "partially" rules, targeting only the "mixed" attribute value via toHaveAttribute/toHaveProperty selectors
  • All rules include auto-fix support
  • New rules are filtered out of the recommended config via a newRules exclusion list in src/index.js

Note

This PR depends on #420 (fix/prefer-checked-mixed-value), which adds excludeValues support to createBannedAttributeRule. Both prefer-pressed and prefer-checked use excludeValues: ["mixed"] to avoid incorrectly flagging aria-pressed="mixed" / aria-checked="mixed". Merge #420 first, then rebase this branch.

Checklist:

  • Documentation
  • Tests
  • Ready to be merged

Testing Instructions

  1. npm install && npm test — all 586 tests should pass
  2. Create a test file with these patterns and verify the rules fire and auto-fix:
    expect(el).toHaveAttribute('aria-pressed', 'true')   // → toBePressed()
    expect(el).toHaveAttribute('aria-pressed', 'mixed')  // → toBePartiallyPressed()
    expect(el).toHaveAttribute('aria-checked', 'mixed')  // → toBePartiallyChecked()
  3. Verify aria-pressed="mixed" is not flagged by prefer-pressed

@shamrt-v shamrt-v marked this pull request as ready for review February 12, 2026 20:47
@G-Rath
Copy link
Collaborator

G-Rath commented Feb 12, 2026

Yeah changing the configs is a breaking change, so please revert that - it would also be good if you can cherry-pick out the bug fix into its own PR since that should be landable on its own.

Also just FYI we're currently blocked on releasing new versions until @benmonro gets the pipeline set back up (see #417), so while I'm happy to review PRs, I may hold off landing them depending on the changes to avoid having too many unreleased changes

@shamrt-v shamrt-v marked this pull request as draft February 12, 2026 21:11
Allow attribute rules to skip specific values (e.g. 'mixed') so
tri-state aria attributes are not incorrectly flagged. Apply to
prefer-checked to stop suggesting not.toBeChecked() for
aria-checked='mixed'.
Verify aria-checked='mixed' is not flagged by prefer-checked.
Suggest toBePressed()/not.toBePressed() instead of manual
toHaveAttribute/toHaveProperty checks on aria-pressed. Excludes
'mixed' value (handled by prefer-partially-pressed).
Shared rule factory for suggesting partially-state matchers
(toBePartiallyChecked, toBePartiallyPressed) when code checks
a tri-state aria attribute for the 'mixed' value.
Suggest toBePartiallyPressed() instead of
toHaveAttribute('aria-pressed', 'mixed').
Suggest toBePartiallyChecked() instead of
toHaveAttribute('aria-checked', 'mixed'). Fills a pre-existing
gap—the matcher has been in jest-dom for years without a
corresponding lint rule.
New rules (prefer-pressed, prefer-partially-pressed,
prefer-partially-checked) are in the 'all' config only,
avoiding a breaking change for existing users.
@shamrt-v shamrt-v force-pushed the feat/new-matcher-rules branch from 9ca36e5 to cafd6c7 Compare February 12, 2026 21:26
@shamrt-v shamrt-v changed the title feat: add rules for new jest-dom matchers (toBePressed, toBePartiallyPressed, toBePartiallyChecked) feat: add rules for toBePressed, toBePartiallyPressed, toBePartiallyChecked matchers Feb 12, 2026
@shamrt-v shamrt-v marked this pull request as ready for review February 12, 2026 21:35
@shamrt-v
Copy link
Contributor Author

shamrt-v commented Feb 12, 2026

it would also be good if you can cherry-pick out the bug fix into its own PR

Alrighty. Done: #420

@G-Rath
Copy link
Collaborator

G-Rath commented Feb 13, 2026

Thanks, I've landed #420.

For the "partially" support though I'm not sure if they need dedicated rules, since the point of them is "use this dedicated matcher, rather than a generic attribute check" - I don't really see a situation where someone would prefer using e.g. toBeChecked but not toBePartiallyChecked?

I wouldn't be surprised if it's more complicated to implement that due to the way this plugin uses functions to build its rules, but I'd be keen to see more of what it'd actually look like to go that route before deciding if we should add new rules instead.

In the meantime, I'd be happy to add prefer-pressed since we'll end up with that either way and we can just have it ignore toBePartiallyPressed like we're now doing with prefer-checked.

Either way fwiw new rules should generally have their own PRs unless they have a lot of overlap in their implementation, to make it easier to review and so that they have their own changelog entry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants