React-testing-library: Cleanup not running in v9

Created on 15 Aug 2019  路  11Comments  路  Source: testing-library/react-testing-library

  • react-testing-library version: 9.1.1
  • react version: 16.9.0
  • node version: 10.16.0
  • npm (or yarn) version: N/A

Relevant code or config:

// jest.setup.js
import { configure as configureRTL } from '@testing-library/react';
configureRTL({ testIdAttribute: 'data-test-id' });
// some-component.test.js
it('test 1', () => {
  const { getByText } = render(<>Hello</>);

  expect(getByText('Hello')).not.toBeNull();
});

it('test 2', () => {
  const { getByText } = render(<>Hello</>);

  expect(getByText('Hello')).not.toBeNull();
});

What you did:

Upgraded to v9

What happened:

Automatic cleanup isn't run which causes tests to break. Seems to be linked caused by the configure fn, when removing it from our setup file cleanup works as expected 馃

When adding

import {render, cleanup} from '@testing-library/react'

afterEach(cleanup)

it works as expected

Reproduction:

I wasn't able to get Codesandbox to honor the jest config, so no repro unfortunately.

Problem description:

Cleanup doesn't run

Suggested solution:

Cleanup should run

Most helpful comment

Yes, you are right about that error, thanks.

But just try to run all tests once again, and you will get an error.

Found multiple elements with the text: /Submit/i

And you will see that component were render several times
Screenshot 2020-03-06 at 14 02 57

All 11 comments

Ah found the issue while creating the repro, the issue is that we used setupFiles instead of setupFilesAfterEnv for setting the RTL config.

Not sure if this should be considered a bug or not. It's a simple fix once you know whats wrong

Thanks for digging in. It's not a bug. Working as documented 馃榾 glad you worked it out!

@alexandernanberg I'm running into the same issue after upgrading to version 9.1.1
Would you mind sharing your RTL config?

@schreyerpeter The RTL config is in my first comment, do you mean the jest config?

@alexandernanberg Yes, Jest config. Thanks!

I've create a sandbox with the issue, seems it's still remaining

https://codesandbox.io/s/green-platform-5fhd1

I've create a sandbox with the issue, seems it's still remaining

codesandbox.io/s/green-platform-5fhd1

@artem-galas

-fireEvent.click(getByText(/Submit/i)[0]);
+fireEvent.click(getByText(/Submit/i));

getByText already returns a single element. The error message included that you didn't pass a valid element. Why did you think this relates to cleanup?

Yes, you are right about that error, thanks.

But just try to run all tests once again, and you will get an error.

Found multiple elements with the text: /Submit/i

And you will see that component were render several times
Screenshot 2020-03-06 at 14 02 57

I think that's a bug in codesandbox. Are you seeing this locally?

Just tried that locally, seems everything is fine with the example above.... 馃

But something went wrong with my real-world project... Will try to find out what's wrong.

Thanks for your time. Sorry for the buzz.

Was this page helpful?
0 / 5 - 0 ratings