Why user-event click() promise resolves only by timeout when injectGlobals: false in Jest config?

53 Views Asked by At

I try to move my Jest tests of react library to injectGlobals: false.

I use user-event for simulate user events and jsdom for simulate browser environment.

Here is an example of issue: https://github.com/krutoo/testing-library-user-event-with-fake-timers

In this repository i have just regular test.

In this test there is call of userEvent.setup().click():

describe("ClickCounter", () => {
  beforeEach(() => {
    jest.useFakeTimers();
  });

  afterEach(() => {
    jest.runOnlyPendingTimers();
    jest.useRealTimers();
  });

  test("should increment value by click", async () => {
    const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
    const { container, getByTestId } = render(<ClickCounter />);

    expect(container.textContent).toBe("Count: 0");

    await user.click(getByTestId("counter"));
    expect(container.textContent).toBe("Count: 1");
  });
});

When injectGlobals: false in Jest config file it just doesn't work.

I have error:

thrown: "Exceeded timeout of 5000 ms for a test.
    Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout.

Why this happens and how to fix that?

0

There are 0 best solutions below