Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
khamer1410 opened this issue Nov 18, 2022 · 1 comment · Fixed by #1391
Closed

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

khamer1410 opened this issue Nov 18, 2022 · 1 comment · Fixed by #1391

Comments

@khamer1410
Copy link

khamer1410 commented Nov 18, 2022

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!
  });
@ph-fritsche
Copy link
Member

Since v14.1 you can use the advanceTimers option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants