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.

How to use `getByTestId` on web?

See original GitHub issue

I believe there was recently a merge to make sure getByTestId only returns native elements. https://github.com/callstack/react-native-testing-library/pull/324

Does this mean we can’t use getByTestId on web?

In my test file in web says no element matches the testID, where as in iOS it’ finds the element and my test passes. If I switch to use a queryByText query, it works fine on both platforms. But I’m targeting a plain react native <View> to check the styling on it is expected, so I need to target by testID.

  // Fails to find ID
  if (Platform.OS === 'web') {
    test('StickyHeader should have sticky position prop on web', () => {
      const { getByTestId } = renderRoute(<AuthenticatedApp logOut={jest.fn} user={user} />)
      const stickyHeader = getByTestId('sticky-header')
      expect(stickyHeader).toHaveStyle({ position: 'sticky' })
    })
  } 

  // Passes
  if (Platform.OS === 'ios') {
    test('StickyHeader should have relative position prop on ios ', () => {
      const { getByTestId } = renderRoute(<AuthenticatedApp logOut={jest.fn} user={user} />)
      const stickyHeader = getByTestId('sticky-header')
      expect(stickyHeader).toHaveStyle({ position: 'relative' })
    })
  }

debug

// prints out the screen structure and I can see an object with `testID='sticky-header'`
// it also includes style object with `position: 'sticky'`, so I can see it does exist
const { debug } = renderRoute(<AuthenticatedApp logOut={jest.fn} user={user} />)
debug()

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mitramejiacommented, Jan 19, 2022

@conor909 can you expand a bit? what was the lack of understanding you realized? I am facing the same issue

1reaction
conor909commented, Aug 13, 2021

Realize this was a lack of understanding

Read more comments on GitHub >

github_iconTop Results From Across the Web

ByTestId - Testing Library
ByTestId functions in DOM Testing Library use the attribute data-testid by default, following the precedent set by React Native Web which uses a ......
Read more >
Using getByTestId in react - YouTube
In this video, we look at what the getByTestId function from @testing-library/react is and where can it be used.
Read more >
Get Started with React Testing Library and Jest
It's another way to get an element from the DOM tree having a particular data-testid attribute.(More about getByTestId from RTL documentation). We are...
Read more >
getByTestId JavaScript and Node.js code examples - Tabnine
Best JavaScript code snippets using getByTestId(Showing top 15 results out of 315) · test/checkout. · test/checkout. · test/line-items. · src/__tests__/react-redux.
Read more >
How to use the @testing-library/dom.getByTestId function in ...
Popular JavaScript code snippets. Find secure code to use in your application or website. which function is used to parse a string to...
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