Add .some
, .every
property chain to matcher that expects an array
#7629
Labels
.some
, .every
property chain to matcher that expects an array
#7629
Clear and concise description of the problem
Currently, when testing elements returned as an array (e.g.,
screen.getAllByRole
in Testing Library), matchers cannot be used directly on individual elements within the array. Specifically, there is no built-in way to check if at least one element satisfies a matcher without manually iterating and extracting a boolean value.This creates several issues:
Current workarounds
Using
Array.prototype.some
and a manual boolean assertionThis approach is verbose and does not leverage existing matchers.
Defining a custom matcher (for each assertion type)
This solves the problem, but requires defining a separate matcher for each assertion type, which is not practical for general use.
Also, there is the hassle of checking the implementation if you want to reproduce the matcher of the community.
Suggested solution
Introduce
.some
and.every
as built-in property chains, such asArray.prototype
, so that assertions can be verified to satisfy some or all of the array.Proposed syntax
Benefits:
Eliminates manual boolean extraction
Avoids the need to define custom matchers for each assertion type
Improves readability by making the test’s intent clear
Enables direct use of community matchers (e.g., testing-library/jest-dom)
For symmetry,
.every
could also be introduced to allow assertions to be applied to all elements in an array..every
is not strictly necessary since the combination of for andArray.prototype.every
is already a viable alternative..every
is included in the proposal primarily for consistency with.some
.Alternative
No response
Additional context
The implementation should ensure proper TypeScript support so that
.some
and.every
only appear on arrays.It should be compatible with expect.extend, allowing custom matchers to work seamlessly.
Performance considerations should be taken into account to avoid unnecessary operations when chaining multiple matchers.
By introducing .some (and optionally
.every
for symmetry), this feature would eliminate boilerplate, improve test readability, and reduce the need for repetitive custom matchers.Validations
The text was updated successfully, but these errors were encountered: