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

useFakeTimers no longer mocks out timer functions properly #11447

Closed
jsumnerp opened this issue May 26, 2021 · 6 comments
Closed

useFakeTimers no longer mocks out timer functions properly #11447

jsumnerp opened this issue May 26, 2021 · 6 comments

Comments

@jsumnerp
Copy link

πŸ› Bug Report

With the new fakeTimers the setTimeout, setInterval, clearTimeout, clearInterval functions are no longer properly mocked

● waits 1 second before ending the game

expect(received).toHaveBeenCalledTimes(expected)

Matcher error: received value must be a mock or spy function

Received has type:  function
Received has value: [Function anonymous]

To Reproduce

https://jestjs.io/docs/timer-mocks using the first example

Expected behavior

For timer functions to be mocked or docs to be updated to show how to get the same results

@LeonardDrs
Copy link

LeonardDrs commented May 26, 2021

Worth noting that jest.useFakeTimers('legacy'); still works for this kind of issue for the time being.

@SimenB
Copy link
Member

SimenB commented May 27, 2021

they are mocked, but they are not mock functions. This is on purpose. See #5171 (although that's not the PR that landed, it contains notes of all the differences between "modern" and "legacy" fake timers)

  • Jest added spies to the fake timers automatically. We no longer do that, people need to do e.g. jest.spyOn(global, 'setTimeout') themselves.

@SimenB SimenB closed this as completed May 27, 2021
@donaldpipowitch
Copy link

Would using jest.useFakeTimers('legacy'); really fully restore the behaviour of Jest 26? We have a couple of tests which fail on Jest 27 no matter how I rewrite them.

They look a bit like this:

beforeAll(() => {
  jest.useFakeTimers();
});

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

test('<SomeComponent/>: just an example', async () => {
  const { findByText, getByTestId, queryByText } = render(<SomeComponent />);

  expect(queryByText('...')).not.toBeInTheDocument();

  fireEvent.click(getByTestId('...'));
  act(() => {
    jest.advanceTimersByTime(5000);
  });

  expect(await findByText('...')).toBeInTheDocument();
});

@SimenB
Copy link
Member

SimenB commented Jun 5, 2021

Both timer systems are the same as in Jest 26, we've just flipped the default implementation

@kenlyon
Copy link

kenlyon commented Jun 5, 2021

Since this is closed, does that mean the docs have been updated to explain the need to call jest.spyOn(global, 'setTimeout')? Without that, I think you’re going to continue to get confused users and more duplicates of this bug. They docs for 27.0 effectively still document legacy mode.

@github-actions
Copy link

github-actions bot commented Jul 6, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants