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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot expect(setTimeout) without legacy mode of useFakeTimers() #11521

Closed
kenlyon opened this issue Jun 3, 2021 · 5 comments
Closed

Cannot expect(setTimeout) without legacy mode of useFakeTimers() #11521

kenlyon opened this issue Jun 3, 2021 · 5 comments

Comments

@kenlyon
Copy link

kenlyon commented Jun 3, 2021

馃挜 Regression Report

I upgraded to jest 27.0 and found my tests failed due to the change in useFakeTimers().

The error is:

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

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

It does work when I use the "legacy" option, however my usage is the recommended approach. I don't think it constitutes the "subtle implementation differences" described in your blog.

Last working version

Worked up to version: 26.6.3

Stopped working in version: 27.0.4 (Probably 27.0.0)

To Reproduce

Steps to reproduce the behavior:

  1. In a test file, call jest.useFakeTimers("modern"); (or jest.useFakeTimers() in v27+)
  2. Attempt to use setTimeout as a mock, such as expect(setTimeout).toHaveBeenCalledTimes(0);

The problem might only happen on Windows, as I couldn't reproduce it in repl and everything else was the same.

Expected behavior

It should still work, as this is the technique described in your docs.

Link to repl or repo (highly encouraged)

repl.it demo

NOTE: The repl runs without error, but fails locally on my Windows 10 workstation. I confirmed I have an identical package-lock.json and have cleaned my npm cache etc. Might it be only an issue on Windows?

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: Windows 10 10.0.19041
    CPU: (8) x64 Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
  Binaries:
    Node: 14.16.0 - C:\Program Files\nodejs\node.EXE    
    Yarn: 1.22.10 - C:\Program Files\nodejs\yarn.CMD    
    npm: 6.14.11 - C:\Program Files\nodejs\npm.CMD      
  npmPackages:
    jest: ^27.0.4 => 27.0.4
@Smrtnyk
Copy link

Smrtnyk commented Jun 4, 2021

seems like a duplicate of #11500

@kenlyon
Copy link
Author

kenlyon commented Jun 4, 2021

@Smrtnyk They might be related, but I'm describing a different symptom. I don't know if setTimeout is working or not. I don't get past expect(setTimeout) because the fake version of setTimeout does not appear to be a valid mock.

@kenlyon
Copy link
Author

kenlyon commented Jun 4, 2021

@Smrtnyk I should have read through the other thread before responding. The discussion does seem to be going in the direction I thought this one might, so there is some duplication in that sense. It's the same underlying cause, but reported via a different use case. I see pros and cons to "duplicate" vs "related" but I don't care to split hair over it. :) I'm just glad that others are seeing the same thing.

It's weird that it works in my repl.it though.

@SimenB
Copy link
Member

SimenB commented Jun 5, 2021

Duplicate of #11447

@SimenB SimenB marked this as a duplicate of #11447 Jun 5, 2021
@SimenB SimenB closed this as completed Jun 5, 2021
@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

3 participants