Skip to content

Previous render sometimes leaking into next test #716

Closed
@ljosberinn

Description

@ljosberinn
  • @testing-library/react version: 10.3.0
  • Testing Framework and version: jest 26.0.1
  • DOM Environment: jsdom 16.2.2

Relevant code or config:

https://codesandbox.io/s/great-dawn-s229i?file=/src/ServiceWorker.test.tsx

it('renders a toast onupdatefound triggering site reload on click', async () => {
    const mockReload = jest.fn();

    Object.defineProperty(window, 'location', {
      value: {
        reload: mockReload,
      },
      writable: true,
    });

    makeMockRegister({
      addEventListener: makeMockAddEventListener(),
      registration: jest.fn(),
      shouldReject: false,
    });

    render(<ServiceWorker />);

    const toast = await screen.findByRole('alert');

    fireEvent.click(toast);

    expect(mockReload).toHaveBeenCalledTimes(1);
  });

What you did:

Trying to write tests in the order the ServiceWorker executes.

What happened:

screen.findByRole('alert') fails because multiple elements with that query are in the DOM.

Reproduction:

https://codesandbox.io/s/great-dawn-s229i?file=/src/ServiceWorker.test.tsx

Problem description:

If I disable all other tests except that one, it works.

image

Changing the order of the tests works too; the test marked as problematic test currently is the 5th/last.

It works locally if its the 4th:
image

Sandbox does not like the 4th...
image

...but as 3rd
image

Adding cleanup or manually calling unmount() does not help at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions