Skip to content

UserEvent: UserJest fake timers require a specific user setup #1180

Closed
@khamer1410

Description

@khamer1410

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!
  });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions