Skip to content

When using waitFor the jest.setSystemTime is reset to actual system time #1001

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

Closed
coreyjv opened this issue Aug 4, 2021 · 2 comments
Closed

Comments

@coreyjv
Copy link

coreyjv commented Aug 4, 2021

What you did:

Attempting to use fake timers and assert those dates and times after using waitFor

What happened:

The system time was reset to the current time after waitFor.

Reproduction:

import { render, screen, waitFor } from '@testing-library/react';

beforeEach(() => {
  jest.useFakeTimers('modern');
  jest.setSystemTime(new Date("2023-02-18"));
})

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

test('test the fake timer', async () => {
  render((<p>text</p>));

  await waitFor(() => screen.getByText('text'))

  expect(new Date().getFullYear()).toEqual(2023)
});

Problem description:

Test results:

  ● test the fake timer

    expect(received).toEqual(expected) // deep equality

    Expected: 2023
    Received: 2021

      16 |   await waitFor(() => screen.getByText('text'))
      17 |
    > 18 |   expect(new Date().getFullYear()).toEqual(2023)
         |                                    ^
      19 | });
      20 |

When using waitFor it appears that the system time that was set using Jest's fake timers was reset to the current time. If you remove the waitFor the test passes as expected.

@eps1lon
Copy link
Member

eps1lon commented Aug 5, 2021

This was fixed in #966 (released in 8.x)

@eps1lon eps1lon closed this as completed Aug 5, 2021
@coreyjv
Copy link
Author

coreyjv commented Aug 5, 2021

Thank you!

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

No branches or pull requests

2 participants