react-testing-library icon indicating copy to clipboard operation
react-testing-library copied to clipboard

After switching to the scoped package, cleanup is no longer working

Open kelly-tock opened this issue 7 years ago • 9 comments

  • react-testing-library version:
    "@testing-library/dom": "^5.1.1",
    "@testing-library/react": "^8.0.1",
  • react version: 16.8.6
  • node version: 11.12.0
  • npm (or yarn) 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:

kelly-tock avatar Jun 02 '19 12:06 kelly-tock

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 😕

kentcdodds avatar Jun 02 '19 12:06 kentcdodds

wow we must have similar schedules.

kelly-tock avatar Jun 02 '19 12:06 kelly-tock

i'll create a small repro. I literally just upgraded/switched over.

kelly-tock avatar Jun 02 '19 12:06 kelly-tock

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.

kelly-tock avatar Jun 02 '19 13:06 kelly-tock

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.

kelly-tock avatar Jun 02 '19 13:06 kelly-tock

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.

2WheelCoder avatar Sep 19 '19 17:09 2WheelCoder

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.

mudrila avatar Nov 05 '19 14:11 mudrila

Another one here, having the same issue

Dashue avatar Apr 08 '20 19:04 Dashue

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!! :)

kentcdodds avatar Apr 08 '20 21:04 kentcdodds