Closed martenmatrix closed 2 years ago
React Testing Library has an auto-cleanup (cleanup) that runs after every test.
It's better to call the render
method in every test suite, instead of one single time.
test('get the heading', () => {
render(
<>
<h2>Test</h2>
<img alt="a cool project 1 being used" />
</>,
)
screen.getByRole('heading')
})
test('get the image by alt text', () => {
render(
<>
<h2>Test</h2>
<img alt="a cool project 1 being used" />
</>,
)
screen.getByAltText(/a cool project 1 being used/i)
})
Or, add multiple asserts in a single suite:
test('get the heading', () => {
render(
<>
<h2>Test</h2>
<img alt="a cool project 1 being used" />
</>,
)
screen.getByRole('heading')
screen.getByAltText(/a cool project 1 being used/i)
})
Sorry and thanks!
@testing-library/dom
version: 8.13.0create-react-app v.5.0.0
and did not find anything in thereact-scripts
folder.Relevant code or config:
What you did:
I've created two tests that both query an element from the rendered body. It is important that the test which gets the element by a role is before the test which gets its element by the text contained in the
alt
attribute.What happened:
If the test which uses
getByRole
is skipped or runs after the test usinggetByAltText()
nothing happens.However, if all tests run,
getByAltText()
fails with the following error message. It is important that the test usinggetByAllText()
runs after another test using a query.Reproduction:
npx create-react-app@5.0.0
name.test.js
and paste the code abovenpm test name.test.js
Problem description:
The test fails because it is unable to find the element, however, the element exists and is also shown when running
screen.debug()
.Suggested solution:
I'm sorry. :(