Automatic unmount of rendered components at end of each test
See original GitHub issueDescribe the Feature
I’m testing hooks that modify the global state of a library, so they need to be unmounted before the next test begins. Currently, I’m using this workaround:
import { render as renderImpl, RenderAPI } from 'react-native-testing-library'
const rendered: RenderAPI[] = []
const render: typeof renderImpl = (...args) => {
const elem = renderImpl(...args)
rendered.push(elem)
return elem
}
beforeEach(() => {
rendered.forEach(elem => elem.unmount())
rendered.length = 0
})
Ideally, react-native-testing-library
would export a function that lets developers opt-in to this behavior, like the following:
import { setAutoUnmount } from 'react-native-testing-library'
setAutoUnmount(true)
Then I wouldn’t need to maintain this code myself. 😃
Possible Implementations
See the workaround.
Related Issues
None.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Is it necessary to call unmount after each test cases in react ...
I asked this because i saw a custom implementation for render in test utils to unmount component after each test cases, and I'm...
Read more >API | Testing Library
This will cause the rendered component to be unmounted. This is useful for testing what happens when your component is removed from the...
Read more >Testing Recipes - React
For each test, we usually want to render our React tree to a DOM element that's attached to document . This is important...
Read more >Upcoming Changes to Component Testing - Cypress
As part of Cypress 11, we are making some small changes to the mount API, the main way you render components. These changes...
Read more >Test Unmounting a React Component with React Testing Library
Sometimes your react component may need to do some cleanup work in the return value from useEffect or useLayoutEffect, or the componentWillUnmount lifecycle ......
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
v1 #237 v2 #238
Um, why not leveraging Jest APIs then?: