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

jest.setTimeout() is ignored if run async'ly in beforeAll() hook #9359

Closed
d4vidi opened this issue Dec 31, 2019 · 6 comments
Closed

jest.setTimeout() is ignored if run async'ly in beforeAll() hook #9359

d4vidi opened this issue Dec 31, 2019 · 6 comments

Comments

@d4vidi
Copy link
Contributor

d4vidi commented Dec 31, 2019

🐛 Bug Report

jest.setTimeout() works if applied in a beforeAll() hook in a script specified in setupFilesAfterEnv, but does not work if applied from within or after an async execution inside beforeAll().

More illustratively, with the following code the timeout gets properly registered:

beforeAll(async () => {
  jest.setTimeout(60000);
  await doSomeInit();
});

However, replacing the two execution lines will prevent the timeout from being registered:

beforeAll(async () => {
  await doSomeInit();
  jest.setTimeout(60000);
});

Similarly, this doesn't work either:

beforeAll(async () => {
  await new Promise((resolve) => {
    jest.setTimeout(60000);
    resolve();
  });
});

i.e. you end up with the infamous Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout. error message and a test failure.

To Reproduce

  1. Set up a project with jest and 1 unit test that takes 6 seconds to complete.
  2. Work out a config.json (i.e. jest config file) with setupFilesAfterEnv set to ["init.js"].
  3. Inside init.js, specify a beforeAll() as explained above - setting the timeout to over 6 seconds.

Expected behavior

jest.setTimeout() applied in the 2 failing steps mentioned above should be registered.

Note: I initially had some thoughts about whether I should report this in light of what's already been reported in issue #6557, alongside the associated PR - #6817, but then I noticed this: Although the documentation specifying the limitations regarding jest.setTimeout() isn't entirely consistent over time, it nevertheless in no point mentions this specific use case. Rather, it limits the scope of where this should be executed (i.e. jest setup), and as for when - it only states "before the test runs", which makes this use case legitimate.

Link to repl or repo (highly encouraged)

N/A

envinfo

  System:
    OS: macOS Mojave 10.14.6
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Binaries:
    Node: 10.15.3 - ~/.nvm/versions/node/v10.15.3/bin/node
    Yarn: 1.3.2 - /usr/local/bin/yarn
    npm: 6.13.2 - ~/.nvm/versions/node/v10.15.3/bin/npm
  npmPackages:
    jest: 24.9.x => 24.9.0
@matus-sabo
Copy link

matus-sabo commented Sep 14, 2021

If u specify timeout as second parameter in beforeAll function, it works as expected

beforeAll(async () => {
    // hook logic
}, 10000);

@d4vidi
Copy link
Contributor Author

d4vidi commented Sep 19, 2021

@lamanabie thanks. That only affects the timeout of the before-all clause itself, rather than applying the timeout over that and any future clauses (e.g. test()s), globally.

htdat added a commit to Automattic/woocommerce-payments that referenced this issue Aug 5, 2022
@VinGarcia
Copy link

VinGarcia commented Dec 28, 2022

I see this is an old ticket, but I am still facing the same issue, I tried the proposed solution but I still get this error:

    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."

That said, even though adding the timeout as the second argument of the beforeAll() call didn't work I tried adding the timeout as the third argument of the it() function on the test that was failing, and this time it worked, i.e.:

it("should do something", async () => {
    // ...
}, 10000)

shazron added a commit to adobe/aio-lib-events that referenced this issue Jul 12, 2023
shazron added a commit to adobe/aio-lib-analytics that referenced this issue Jul 12, 2023
adrienjoly added a commit to openwhyd/openwhyd that referenced this issue Jul 24, 2023
adrienjoly added a commit to openwhyd/openwhyd that referenced this issue Jul 24, 2023
* docs: find functions to which the deprecated `fields` option is passed

* fix: trying the use of `project` on models/user.js::fetchMulti()
cf https://stackoverflow.com/a/51732851/592254

* fix(tests): Exceeded timeout of 5000 ms for a hook
cf https://github.com/openwhyd/openwhyd/actions/runs/5646589314/job/15294832053?pr=628#step:5:47

* fix: use `project` on models/post.js::fetchPosts()

* fix: use `project` on models/mongodb.js::forEach()

* fix: use `project` on models/mongodb.js::forEach2()

* fix `Exceeded timeout of 5000 ms for a hook`
cf jestjs/jest#9359 (comment)

* fix: use `project` on models/follow.js::fetchArray()

* fix: remove TODOs fixed by previous commit

* fix: use `project` on models/comment.js::fetch()

* fix: use `project` on controllers/admin/index.js::countDbUsersAndPlaylists()

* fix: use `project` on controllers/admin/index.js::refreshIndex()
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 Dec 28, 2023
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 Jan 27, 2024
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 Feb 27, 2024
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

4 participants