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.

Automatic unmount of rendered components at end of each test

See original GitHub issue

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

github_iconTop GitHub Comments

3reactions
aleclarsoncommented, Nov 19, 2019

v1 #237 v2 #238

3reactions
thymikeecommented, Nov 19, 2019

so they need to be unmounted before the next test begins

Um, why not leveraging Jest APIs then?:

describe('testing hook that messes with globals', () => {
  let component;
  afterEach(() => component.unmount())
	
  test('1', () => {
    component = render(element1)
  })

  test('2', () => {
    component = render(element2)
  })
})
Read more comments on GitHub >

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

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