How to use `getByTestId` on web?
See original GitHub issueI 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:
- Created 2 years ago
- Comments:12 (2 by maintainers)
Top 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 >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
@conor909 can you expand a bit? what was the lack of understanding you realized? I am facing the same issue
Realize this was a lack of understanding