Layer component stays rendered in test suites
See original GitHub issueExpected Behavior
When testing the Layer
component with React-Testing-Library each instance of test should render a new component.
Actual Behavior
The Layer component remains rendered to the testing DOM despite providing a cleanup function afterEach(cleanup)
. This causes tests to fail as duplicates of the component are found.
URL, screen shot, or Codepen exhibiting the issue
https://codesandbox.io/s/grommet-v2-template-forked-66prc?file=/modal.test.js
CodeSandbox is having issues running tests but this is an example.
Steps to Reproduce
Create a component utilizing Grommet’s Layer component, run test with React-Testing-Library, notice the Layer remains rendered across multiple tests.
Your Environment
Only noticed after bumping to Grommet 2.17.2
- Grommet version: 2.17.2
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
How to fire events on multiple layers of custom components ...
So most tests should look something like this. const user = userEvent.setup() render(<MyComponent/>) await ...
Read more >React Testing Techniques. This blog focuses on the 'how' of…
Continuing to build upon the idea of “Test your components as a user would”, a key strategy is to prefer integration testing over...
Read more >Testing a simple component with React Testing Library
First I will render the component and see that it's actually on screen. I will create a new test file called: AddWord.test.js and...
Read more >React Component Tests for Humans - CSS-Tricks
Component tests flow through three phases: Arrange: The component props are prepared. Act: The component needs to render its DOM to the UI...
Read more >Testing React Components With Enzyme
I'll cover a few cases where mount is the better choice later. Simulating Change Events With Enzyme. Rendering a component with Enzyme's shallow ......
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
Hey Andrew! Thanks so much for putting the repo together and documenting this so clearly. @jcfilben, @britt6612, and I have been looking into this today, and I wanted to summarize our findings for you here:
cleanup
function doesn’t handle it (https://stackoverflow.com/questions/67298821/jest-react-testing-library-portals-are-not-being-cleaned-up). Grommet does this in a function calledcreatePortal
which ensures that the DOM is cleared out before the test: https://github.com/grommet/grommet/blob/master/src/js/utils/portal.js#L4Within our Layer-test file, we call
beforeEach(createPortal)
Because your testing setup doesn’t include thisbeforeEach
function, the portal is not being cleared out between tests. This is unrelated to grommet 2.17.2. I cloned your repo and reverted the dependency versions to match the react, react-dom, testing-library, and grommet versions that we used at grommet v2.16.3 and saw the tests failing with the same errors as you’re experiencing here. However, once I added thebeforeEach
everything is working as expected.WITH beforeEach:
It seems like the reason your project was passing prior but not with grommet v2.17.1 or 2.17.2 is because React 16 and lower treated
useEffect
differently (synchronously) thanuseEffect
is treated in React 17 (asynchronously). Given this, we made an adjustment in grommet v2.17.1 to Layer to useuseLayoutEffect
(which runs synchronously like useEffect had in older versions of React).Recommendation: Add the following to your jest
In react-dom v17
useEffect
is changed to run asynchronously, so we switched Layer to useuseLayoutEffect
which runs synchronously which is the old behavior ofuseEffect
. This change fixed an issue with the Layer animation but it don’t think it would be related to the tests and I don’t think it would cause the Layer to persist between tests.I did notice in the codesandbox that was shared above the versions for the
react
andreact-dom
are v16 instead of v17 so I think this is likely the cause of the problem