question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

queryAllByProps returning incorrect number of elements

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thymikeecommented, Feb 1, 2020

Thanks! FYI, ByProps matchers are going to be deprecated in v2. Please migrate off this API, e.g. to ByText, ById or ByA11y*. We’ll try to fix it for 1.x, but it’s not going to be a priority.

0reactions
thymikeecommented, Feb 1, 2020

@jackharding in this case you can use getAllByTestId. @teo029 thanks for pointing this out. Turns out that the behavior of getByTestId (searching across all components, incorrect) is different than getAllByTestId (searching across host components, correct). However changing this behavior would be pretty breaking, because some of our users use testID to find a specific custom component, which may not pass this prop to a host component like View or Text. 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 in get*ByProps (happy to accept PR adding this check and providing user with a hint to use get*ByTestId

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found