queryAllByProps returning incorrect number of elements
See original GitHub issueI have a test with the following assertion:
expect(queryAllByProps({ testID: ‘searchSuggestion’ })).toHaveLength(1);
I know there is only one element with the testID ‘searchSuggestion’. When I log the result of queryAllByProps, I get the following:
[ ReactTestInstance {
_fiber:
FiberNode {
tag: 1,
key: 'cardiff',
elementType: [Function],
type: [Function],
stateNode: [ReactClassComponent],
return: [FiberNode],
child: [FiberNode],
sibling: null,
index: 0,
ref: null,
pendingProps: [Object],
memoizedProps: [Object],
updateQueue: null,
memoizedState: [Object],
contextDependencies: null,
mode: 0,
effectTag: 5,
nextEffect: [FiberNode],
firstEffect: null,
lastEffect: null,
expirationTime: 0,
childExpirationTime: 0,
alternate: null,
_debugID: 41,
_debugSource: null,
_debugOwner: [FiberNode],
_debugIsCurrentlyTiming: false,
_debugHookTypes: null } },
ReactTestInstance {
_fiber:
FiberNode {
tag: 1,
key: null,
elementType: [Function],
type: [Function],
stateNode: [Component],
return: [FiberNode],
child: [FiberNode],
sibling: null,
index: 0,
ref: null,
pendingProps: [Object],
memoizedProps: [Object],
updateQueue: null,
memoizedState: null,
contextDependencies: null,
mode: 0,
effectTag: 1,
nextEffect: null,
firstEffect: null,
lastEffect: null,
expirationTime: 0,
childExpirationTime: 0,
alternate: null,
_debugID: 42,
_debugSource: null,
_debugOwner: [FiberNode],
_debugIsCurrentlyTiming: false,
_debugHookTypes: null } },
ReactTestInstance {
_fiber:
FiberNode {
tag: 5,
key: null,
elementType: 'View',
type: 'View',
stateNode: [Object],
return: [FiberNode],
child: [FiberNode],
sibling: null,
index: 0,
ref: null,
pendingProps: [Object],
memoizedProps: [Object],
updateQueue: null,
memoizedState: null,
contextDependencies: null,
mode: 0,
effectTag: 0,
nextEffect: null,
firstEffect: null,
lastEffect: null,
expirationTime: 0,
childExpirationTime: 0,
alternate: null,
_debugID: 43,
_debugSource: null,
_debugOwner: [FiberNode],
_debugIsCurrentlyTiming: false,
_debugHookTypes: null } } ]
Am I misunderstanding how this should work? Or is this a bug?
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
How do you test for the non-existence of an element using jest ...
I have a component library that I'm writing unit tests for using Jest and react-testing-library. Based on certain props or events I want...
Read more >React Testing Library Tutorial - Robin Wieruch
queryAllBy ; findAllBy. Whereas all of them return an array of elements and can be associated with the search types again. Assertive Functions....
Read more >Queries | React Native Testing Library - Open Source
findBy queries return a promise which resolves when a matching element is found. The promise is rejected if no elements match or if...
Read more >The Complete Beginner's Guide to Testing React Apps
This is useful for asserting an element that is not present. queryAllBy : returns an array of all matching nodes for a query...
Read more >Testing a React app with React Testing Library (RTL) - Medium
This method also returns the element, but it makes the test fail ... an element by test id, we first need to add...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks! FYI,
ByProps
matchers are going to be deprecated in v2. Please migrate off this API, e.g. toByText
,ById
orByA11y*
. We’ll try to fix it for 1.x, but it’s not going to be a priority.@jackharding in this case you can use
getAllByTestId
. @teo029 thanks for pointing this out. Turns out that the behavior ofgetByTestId
(searching across all components, incorrect) is different thangetAllByTestId
(searching across host components, correct). However changing this behavior would be pretty breaking, because some of our users usetestID
to find a specific custom component, which may not pass this prop to a host component likeView
orText
. We even have tests for that behavior. Will add changing that behavior to v2 release.Closing, as this is not actionable, other than forbid
testID
as a prop to search by inget*ByProps
(happy to accept PR adding this check and providing user with a hint to useget*ByTestId