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

[Possible regression] jest.setTimeout not honored #11500

Closed
coodoo opened this issue May 31, 2021 · 29 comments
Closed

[Possible regression] jest.setTimeout not honored #11500

coodoo opened this issue May 31, 2021 · 29 comments

Comments

@coodoo
Copy link

coodoo commented May 31, 2021

After updating to ^27.0.3 this morning, a bunch of previously working tests started to fail, with error messages below:

thrown: "Exceeded timeout of 5000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

I did have jest.setTimeout(300000) in place and it was working fine before, hence I'm doubting there might be a regression issue here.

As a side note, after adding a global timeout in the jest config the problems went away, hence it's most likely a jest.setTimeout regression, might worth looking into.

@MEsteves22
Copy link

MEsteves22 commented Jun 1, 2021

Hello!

I am currently having the same problem. I tried using jest.setTimeout, testTimeout or even set the timeout on the test but nothing is working...

I had to downgrade jest and babel-jest for now. I downgraded jest to 26.6.3 and babel-jest to 25.5.1. Any version after that broke my tests.

Thank you!

@s100
Copy link

s100 commented Jun 1, 2021

I believe setTimeout is not being mocked correctly in Jest 27. I'm finding that

it('mocks setTimeout correctly', () => {
  jest.useFakeTimers()
  setTimeout(() => {}, 1000)
  expect(setTimeout).toHaveBeenCalled()
})

gives

Error: expect(received).toHaveBeenCalled()

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

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

ocean90 added a commit to wearerequired/lint-action that referenced this issue Jun 2, 2021
@johnhunter
Copy link

I'm seeing the same thing with jest@27.0.3

The new default "modern" timer mocking works as expected. However asserting expect(setTimeout).toHaveBeenCalled... reports the [Function setTimeout] as stated by @s100

Reverting to the legacy timer mocks works ok, e.g.

it('mocks setTimeout correctly', () => {
  jest.useFakeTimers('legacy')
  setTimeout(() => {}, 1000)
  expect(setTimeout).toHaveBeenCalled()
})

@Smrtnyk
Copy link

Smrtnyk commented Jun 2, 2021

I think its due to fact that modern timers use sinon fake timers and jest does not see it as proper mock where legacy timers were created by jest, but not sure if that is the case

ocean90 added a commit to wearerequired/lint-action that referenced this issue Jun 2, 2021
* Bump jest from 26.6.3 to 27.0.3

Bumps [jest](https://github.com/facebook/jest) from 26.6.3 to 27.0.3.
- [Release notes](https://github.com/facebook/jest/releases)
- [Changelog](https://github.com/facebook/jest/blob/master/CHANGELOG.md)
- [Commits](jestjs/jest@v26.6.3...v27.0.3)

Signed-off-by: dependabot[bot] <support@github.com>

* Move `jest.setTimeout()` to top level to fix possible regression

See jestjs/jest#11500

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Dominik Schilling <dominikschilling+git@gmail.com>
@johnhunter
Copy link

@Smrtnyk That fits with what I'm seeing.

If modern timers are not going to conform to the mock signature then the documentation should be updated to reflect it. The Timer Mocks documentation for v26 recommends using the failing approach.
https://jestjs.io/docs/timer-mocks

@AlCalzone
Copy link

Since this issue is labeled "needs repro", I'm going to point to a related issue with a repro:
#11543

zaki-yama added a commit to kintone/js-sdk that referenced this issue Jul 21, 2021
tasshi-me pushed a commit to kintone/js-sdk that referenced this issue Jul 27, 2021
* test: move `jest.setTimeout` to top of the file

ref. jestjs/jest#11500

* chore: extend timeout to 30 sec
@jackson-stone-ableto
Copy link

jackson-stone-ableto commented Oct 29, 2021

We saw this as well with jest.setTimeout for our tests when migrating from 26 -> 27. We had only one particularly long running test and so we were calling that function within the test body to increase the timeout default. (As the CLI suggests!)

We have migrated to passing the timeout argument to the test directly as the trailing argument as well as the afterEach and it appears to have resolved the issue.

@txeto90
Copy link

txeto90 commented Nov 4, 2021

Hello, everyone. I moved jest.setTimeout(600000) to top level and I have an timeout error 600ms.
are there any solution for this issue? Thanks so much.

TimeoutError: Element .breadcrumb-container not found waiting for function failed: timeout 600ms exceeded

@Niryo
Copy link

Niryo commented Dec 28, 2021

looks like if you use sinon-fake-timers directly, it somehow collides with jest.setTimeout and the tests are failing.

@ppamorim
Copy link

I have the same issue, is there any plan to fix that?

@jeffersoneagley
Copy link

Just ran into this and it's now holding up an upgrade for us too.

@ceoworks
Copy link

I'm having the same issue with jest.setTimeout not working.
Jest version in package.json is ^27.4.5

@ppamorim
Copy link

@ceoworks Migrate to the timeout at the it(...) definition.

@AmosSpark
Copy link

Hello!

I am currently having the same problem. I tried using jest.setTimeout, testTimeout or even set the timeout on the test but nothing is working...

I had to downgrade jest and babel-jest for now. I downgraded jest to 26.6.3 and babel-jest to 25.5.1. Any version after that broke my tests.

Thank you!

I'm having this issue currently. Have you been able to solve it without having to downgrade?

@bertrand-caron
Copy link

Got the same issue updating from jest@26 to jest@^28.1.0. Sorry if I sound entitled, but how is this still an issue a year (and a major version) after the original report of this issue?

@s100
Copy link

s100 commented May 23, 2022

Maybe try Sinon?

@AmosSpark
Copy link

AmosSpark commented May 23, 2022

Maybe try Sinon?

So I'm following this tutorial (sinon implementation @11:00) and still got issues.

  1. clock = FakeTimers.install ();
  • gave me an error:
    cannot assign read only property 'performance' of object '#<object>'
  1. I changed to
    clock = FakeTimers.createClock ();
  • got the original errror:
    thrown: "Exceeded timeout of 5000 ms for a test. Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

Although I'm testing a rest api endpoint that finds an item by id (findById).

Do you have any idea of how to go about it? Tried every supposed solution.

tasshi-me pushed a commit to kintone/cli-kintone that referenced this issue Jul 1, 2022
* test: move `jest.setTimeout` to top of the file

ref. jestjs/jest#11500

* chore: extend timeout to 30 sec
tasshi-me pushed a commit to kintone/cli-kintone that referenced this issue Jul 1, 2022
* test: move `jest.setTimeout` to top of the file

ref. jestjs/jest#11500

* chore: extend timeout to 30 sec
tasshi-me pushed a commit to kintone/cli-kintone that referenced this issue Jul 1, 2022
* test: move `jest.setTimeout` to top of the file

ref. jestjs/jest#11500

* chore: extend timeout to 30 sec
@matheustrres
Copy link

matheustrres commented Sep 14, 2022

Any solution for this? Im using jest at 29.0.2 version and facing the same issue. Even using jest.setTimeout or passing testTimeout in jest.config.js, nothing solves it.

@AmosSpark
Copy link

Any solution for this? Im using jest at 29.0.2 version and facing the same issue. Even using jest.setTimeout or passing testTimeout in jest.config.js, nothing solves it.

Are you using custom global error handlers?

For me, I had to do away with my global error handlers in the app.js file.

@matheustrres
Copy link

Any solution for this? Im using jest at 29.0.2 version and facing the same issue. Even using jest.setTimeout or passing testTimeout in jest.config.js, nothing solves it.

Are you using custom global error handlers?

For me, I had to do away with my global error handlers in the app.js file.

Not really, im just creating it and receiving these error.
By the way, instead of "Exceeded timeout of 5000 ms for a test." i receive "[...] for a hook".

@AmosSpark
Copy link

Any solution for this? Im using jest at 29.0.2 version and facing the same issue. Even using jest.setTimeout or passing testTimeout in jest.config.js, nothing solves it.

Are you using custom global error handlers?
For me, I had to do away with my global error handlers in the app.js file.

Not really, im just creating it and receiving these error. By the way, instead of "Exceeded timeout of 5000 ms for a test." i receive "[...] for a hook".

Could you share the hosted link?

@johnhunter
Copy link

As of jest 29 legacy timers no longer seem to work. Did I miss that in the release notes?

jest.useFakeTimers('legacy')
...
console.log(setTimeout.mock.calls); // -> undefined

@mrazauskas
Copy link
Contributor

The API has changed since Jest 28. See Upgrade Guide: https://jestjs.io/docs/28.x/upgrading-to-jest28#fake-timers

@johnhunter
Copy link

As of jest 29 legacy timers no longer seem to work. Did I miss that in the release notes?

So adding a separate spy to the non-legacy fake timer seems to work:

jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');

@mrazauskas
Copy link
Contributor

Yep. Just as documented here: https://jestjs.io/docs/timer-mocks

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Sep 16, 2023
@github-actions
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2023
@MattyBalaam
Copy link

Feels like a rather large bug which will negatively affect people who use Jest for testing UI where tests can easily exceed 5 seconds.

Copy link

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 Nov 16, 2023
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