-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
beforeAll() executed in parallel with test case in case of timeout #9527
Comments
Still occuring with jest 25.1.0. |
Seems I've found a workaround here. Since I wrapped my test case and hooks with a |
@dfleury I do not observe the same behaviour: % cat test/t.spec.js
describe('main', () => {
beforeAll(async () => new Promise((resolve, reject) => {
// Never resolves
}));
test('test case', async () => {
console.log('should never be executed');
});
});
% npx jest test/t.spec.js
FAIL test/t.spec.js (5.296s)
main
✕ test case (5ms)
● main › test case
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
at mapper (node_modules/jest-jasmine2/build/queueRunner.js:25:45)
console.log test/t.spec.js:7
should never be executed
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 5.646s, estimated 6s
Ran all test suites matching /test\/t.spec.js/i.
% npx envinfo --preset jest
npx: installed 1 in 0.727s
System:
OS: Linux 5.3 Manjaro Linux
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Binaries:
Node: 13.7.0 - /usr/bin/node
Yarn: 1.22.0 - /usr/bin/yarn
npm: 6.13.6 - /usr/bin/npm
npmPackages:
jest: ^25.1.0 => 25.1.0
|
I guess the problem in your example is you're never resolving the promise and the global timeout is firing because of that. |
We have been bitten by this unexpected behaviour as well, here is a simple repro case where the log output will show the improper sequencing
Output: |
Same with using |
I have the same issue. Planned on using a The parallel execution of Anyone found a good solution to this? |
I'm facing this issue. |
Am I understanding this correctly that jest is basically telling that you shouldn't share context between tests at all? For example, with puppeteer/playwright it's commonly documented to do describe.each(["chromium", "firefox"])("%s", (browserType) => {
let browser;
let page;
beforeAll(async () => {
browser = await playwright[browserType].launch();
});
afterAll(async () => {
await browser.close();
});
beforeEach(async () => {
page = await browser.newPage();
});
afterEach(async () => {
await page.close();
});
it('first test', async () => {
// do something with `page`
});
it('second test', () => {
// do something with a new `page` that doesn't interfere with the first test
});
}); With It's not a blocker since I can create a new browser and page per test but then what is the point of |
Our company lost 4 man-days of development because of this bug. Please fix this so others don't suffer the same fate. Return proper error for this and don't run test if |
Anyone found any workaround for this? I'm facing the same issue in beforEach() when running playwright tests |
@hananmalka Use bigger timeout. beforeEach should also support timeout in milliseconds as second argument. So example |
@wanton7 I tried this one but it didn't work. |
@hananmalka very odd because it works for |
@wanton7
And this is the error I get:
According to this error it looks like it tries to do the actions inside the "it" before the "beforeEach" ends... I'm using playwright and testRunner: "jasmine2" |
@hananmalka from looks if it, I don't think you even have this issue. I think Promise from |
Use `jest-circus` as test runner. Seems to resolve this issue jestjs/jest#9527 and the issue where a rejecting promise returned from `beforeAll` does not prevent test cases from being run. I had to fix tests cases that have both `done()` and `async` in use in them because circus throws an error if using both in a test. I also increased default test timeout to 10 seconds, as it seems 5 seconds is not enough in many cases.
For anyone else having trouble with this, the problematic behavior comes from the default Jasmine2 test runner. Specifying jest-circus as your test runner will stop tests from running when a yarn jest --testRunner=jest-circus/runner [...other options] Alternatively, add the following to your jest config file: {
//...your other config options
"testRunner": "jest-circus/runner"
} |
@Rossh87 Hmm I was using |
@thernstig Beforeeach and beforeAll work how I would expect, at least in my testing environment. Here's the repo I was using--maybe see if it gives you the same results? I dug through the commit history a bit looking for this specific change to jest-circus without finding anything conclusive, but I didn't spend a ton of effort on it. |
@Rossh87 is right. This can/should be closed now. Using This is what I did to confirm that the scenario describe in this issue does bail if Code beforeAll( // or beforeEach
async () =>
new Promise((resolve, reject) => {
// Never resolves
})
);
test('case', async () => {
console.log('should never be executed');
}); Failure with FAIL UNIT:SERVER server/util/__tests__/test1.unit.test.js (6.838 s)
✕ case
● case
thrown: "Exceeded timeout of 5000 ms for a hook.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
15 | // });
16 |
> 17 | beforeAll(
| ^
18 | async () =>
19 | new Promise((resolve, reject) => {
20 | // Never resolves
at Object.<anonymous> (util/__tests__/test1.unit.test.js:17:1)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 6.868 s Failure with FAIL UNIT:SERVER server/util/__tests__/test1.unit.test.js (6.831 s)
✕ case (5002 ms)
● case
thrown: "Exceeded timeout of 5000 ms for a hook.
Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."
15 | // });
16 |
> 17 | beforeEach(
| ^
18 | async () =>
19 | new Promise((resolve, reject) => {
20 | // Never resolves
at Object.<anonymous> (util/__tests__/test1.unit.test.js:17:1)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 6.859 s, estimated 7 s |
@aalexgabi maybe you can close this? |
Doesn't work for me |
I think showing your code, a minimum repro would be nice since it seems to work for some of us. |
I'm using jest-cucumber to run my tests. In definition of feature, I call const feature = loadFeature(__dirname + '/../features/create_redirect_url.feature')
defineFeature(feature, test => {
setup()
test('Successfully create a url for new user', ({ given, when, then }) => {
given('user is logged in', userIsLoggedIn())
[...] export default function setup() {
beforeAll(async () => {
await startInTestMode() })
beforeEach(async () => {
await Cart.deleteMany({})
nock.cleanAll()
})
afterAll(async () => {
await MongoDbMock.stopDbMock()
})
}
export async function startInTestMode() {
DotenvConfig.configure()
InversifyConfig.configure()
ExpressConfig.configure()
ApplicationConfig.configure()
await MongoDbMock.initDbMock()
} |
Sorry I thought you used plain jest with jest-circus, I have no idea how cucumber works and maybe it is related to that then? Maybe if you try with plain jest and jest-circus, you can instead then redirect your question to the cucumber repo. In addition I assume you are configuring jest-circus like explained here https://www.npmjs.com/package/jest-circus#configure |
I write an issue to the jest-cucumber repository. If someone is having the same problem with jest-cucumber there is the thread: bencompton/jest-cucumber#124 |
apparently jest does not wait for an async function to complete in BeforeAll hooks (see: jestjs/jest#9527). With the default timeout of 5 seconds this had the effect of our test cases being run without initialization to be complete. this was no issue prior as we used to use execSync which blocked the whole js main loop and thereby also blocked jest in running our test cases. switched exec to spawn for some test-helper shell calls. this allows to capture all logs and can also allow to print them as they appear (line per line) which helped discover the cause of the jest related issue
i change de mongoose version to: "mongoose": "5.13.5", and works!! 🤔 |
@SimenB I believe this can be closed, see #9527 (comment) |
@SimenB this should be able to be closed, see my comment above. |
Just FYI, this issue also may have to do with running an older version of Mongo, or that it's running slowly on your local machine. If it's passing in CI, try upgrading and running Mongo with the Docker extension in VSCode to get your tests to pass locally. |
Our company doesn't use Mongo. We use PostgreSQL and we where effected by this until we increased default test timeout about 3 months ago. |
I'm getting tired of all this shit I'ma just start doin shit anyway kind of
way
…On Fri, 22 Apr 2022, 2:27 am wanton7, ***@***.***> wrote:
Our company doesn't use Mongo. We use PostgreSQL and we where effected by
this until we increased default test timeout about 6 months ago.
—
Reply to this email directly, view it on GitHub
<#9527 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AXSL3Z5P7VW6SAUHCAOPGYLVGJWGJANCNFSM4KQ5YSXQ>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
We're seeing a related problem even with jest-circus (but maybe that's intentional, not sure). If beforeAll times out, then afterEach / afterAll get invoked. afterAll I could maybe see making some sense but running afterEach is weird. Jest version 27.4.1 which uses jest-circus by default:
prints
A similar pattern occurs if I would posit that
That way |
beforeEach is fundamentally flawed. it should have a return value that returns isolated resources that are made available to tests as arguments. setting describe-scoped variables kills parallelization. as it stands, it forces serialization, or a million describe blocks, which need to roll their own fixture architecture |
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. |
Stale or not, its still not ok |
🐛 Bug Report
Test case is executed even if beforeAll did not finish.
To Reproduce
This test file:
Displays this:
Expected behavior
Test case code should never be executed because beforeAll() did not finish. The error message should be at the beforeAll level and not at the test case level. This is very confusing as you may think that the test case itself failed when in reality it has nothing to do with the test case.
Link to repl or repo (highly encouraged)
https://repl.it/repls/KindlyAutomaticLocation
envinfo
The text was updated successfully, but these errors were encountered: