testing-library/testing-library-docs

UserEvent: UserJest fake timers require a specific user setup

khamer1410 opened this issue · 1 comments

Is your feature request related to a problem? Please describe.
When using jest fake timers You have to turn off delay in user setup. I haven't found this description in the doxs, have to use: https://onestepcode.com/testing-library-user-event-with-fake-timers/ blog post.

Please add this for future generations, thanks :)

describe('test', () => {
  beforeEach(() => {
    jest.useFakeTimers();
    jest.setSystemTime(new Date('2022-01-01').getTime());
  });

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

  test('When user clicks something is going on', async () => {
    const { getAllByRole } = render(<ComponentWithTimersDependency />);
    const user= userEvent.setup();
    const buttons = getAllByRole('button');
    await user.click(buttons[0]); // this line will run till timeout
  });
});

Describe the solution you'd like
solution:

  test('When user clicks something is going on', async () => {
    const { getAllByRole } = render(<ComponentWithTimersDependency />);
    const userCompatibleWithFakeTimer = userEvent.setup({ delay: null });
    const buttons = getAllByRole('button');
    await userCompatibleWithFakeTimer.click(buttons[0]); // all good!
  });

Since v14.1 you can use the advanceTimers option.