After switching to the scoped package, cleanup is no longer working
react-testing-libraryversion:
"@testing-library/dom": "^5.1.1",
"@testing-library/react": "^8.0.1",
reactversion: 16.8.6nodeversion: 11.12.0npm(oryarn) version: 1.16.0
Relevant code or config:
beforeEach(() => {
store = configureStore({
...initial data
});
});
test('renders without crashing', async () => {
const { getByText, getByTestId, container, debug } = renderWithRedux(
<TagList selectedTags={['PON']} />,
store
);
expect(getByText('PON')).toHaveClass('TagComponent--guest');
expect(container.firstChild).toMatchSnapshot();
});
test('takes selected tags with toggle', async () => {
const { getByText, getByTestId, container, debug } = renderWithRedux(
<TagList selectedTags={['PON']} />,
store
);
expect(container.firstChild).toMatchSnapshot();
});
test('will send the string value of the tag in a list when the tag is clicked and isToggle is true', async () => {
const onClickTag = jest.fn();
const { getByText, getByTestId, container, debug } = renderWithRedux(
<TagList
selectedTags={['PON']}
onClickTag={onClickTag}
/>,
store
);
fireEvent.click(getByText('PON'));
expect(onClickTag).toHaveBeenCalledWith('PON');
});
What you did:
switching to the scoped version of this package
What happened:
tests that were passing under the last version of react-testing library, "react-testing-library": "^7.0.1", are now not passing due to cleanup not working.
<body>
<div>
<div
class="Tag-list"
>
<ul
class="Tag-list-list"
>
<li>
<div
class="TagComponent TagComponent--guest"
>
PON
</div>
</li>
</ul>
</div>
</div>
<div>
<div
class="Tag-list"
>
<ul
class="Tag-list-list"
>
<li>
<div
class="TagComponent TagComponent--guest TagComponent--checked TagComponent--toggle"
>
PON
</div>
</li>
<li>
<div
class="TagComponent TagComponent--guest TagComponent--unchecked TagComponent--toggle"
>
Birthday
</div>
</li>
</ul>
</div>
</div>
<div>
<div
class="Tag-list"
>
<ul
class="Tag-list-list"
>
<li>
<div
class="TagComponent TagComponent--guest"
>
NOP
</div>
</li>
</ul>
</div>
</div>
<div>
<div
class="Tag-list"
>
<span
class="Tag-list-placeholder"
>
No tags available
</span>
</div>
</div>
<div>
<div
class="Tag-list"
>
<span
class="Tag-list-placeholder"
>
No tags for this guest.
</span>
</div>
</div>
<div>
<div
class="Tag-list"
>
<ul
class="Tag-list-list"
>
<li>
<div
class="TagComponent TagComponent--guest TagComponent--checked TagComponent--toggle"
>
PON
</div>
</li>
<li>
<div
class="TagComponent TagComponent--guest TagComponent--unchecked TagComponent--toggle"
>
Birthday
</div>
</li>
</ul>
</div>
</div>
</body>
Found multiple elements with the text: PON
fireEvent.click(getByText('PON'))
some of the tests in between were omitted.
Reproduction:
Problem description:
I also tried changing my jest config to be:
setupFilesAfterEnv: [require.resolve('./setupTestFramework.js'), '@testing-library/react/cleanup-after-each'],
instead of having
require('@testing-library/react/cleanup-after-each');
in the setupTestFramework file mentioned above.
Suggested solution:
That's strange. I haven't had this problem as I've been updating projects and haven't heard from anyone else about this issue either 😕
wow we must have similar schedules.
i'll create a small repro. I literally just upgraded/switched over.
I couldn't get it to repro in the template repository, after upgrading it to latest react, @testing-library, jest, jest dom( I may submit a pr to clean that up).
I found locally though that if I import cleanup directly from testing library in one of the tests thats failing, it no longer fails and cleanup is working.
ok, sorry for the issue. we have a bit of a weird setup with respect to one of our shared modules. before this update, my moduleMap was correctly mapping this to the correct location. but with the change the cleanup was firing from the project root, but the testing library was rendering from a different location entirely.
this is more of a result of our project structure. apologies.
I had an identical issue when upgrading to 9.1.4 from ^7. It turned out to be caused by using jest-axe in the same test as some getBytext calls. Moving jest-axe to a separate test resolved the issue.
Have the same problem, but we don't use jest-axe.
I updated from react-testing-library: ^5.4.4 to the latest @testing-library/react, and in some cases - cleanup doesn't work for me.
I also removed cleanup from each test, cause found out that now it works automatically.
I tried to add import "@testing-library/react/cleanup-after-each"; to my setupTests.js file, but that doesn't change anything.
I don't know what exactly could be source of my problem, but if I would have any updates - I would be happy to bring them here, in case of anyone have the same issue so far.
Another one here, having the same issue
This issue is old and anyone experiencing this today is probably not having the same issue as the original. We'll need more details. Please create a reproduction and a new issue. Thanks!! :)