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

Asynchronous beforeEach / beforeAll? #1256

Closed
tonyxiao opened this issue Jul 7, 2016 · 22 comments · Fixed by #1546
Closed

Asynchronous beforeEach / beforeAll? #1256

tonyxiao opened this issue Jul 7, 2016 · 22 comments · Fixed by #1546

Comments

@tonyxiao
Copy link

tonyxiao commented Jul 7, 2016

Is it supported by jest? Sometimes test setup methods do not run synchronously.

@tonyxiao
Copy link
Author

tonyxiao commented Jul 7, 2016

Also filed an issue in jest. jasmine/jasmine#1145

@cpojer
Copy link
Member

cpojer commented Jul 7, 2016

I don't believe that beforeEach and afterEach will work with async/await without us making modifications to it like we do with it. I have no problem with doing that though and it should be straightforward to make it happen.

If you'd like to send a pull request (with an integration test! See integration_tests/promise_it), here is where you'd look into adding it: https://github.com/facebook/jest/blob/master/packages/jest-jasmine2/src/jasmine-pit.js

@tonyxiao
Copy link
Author

tonyxiao commented Jul 7, 2016

Will definitely take a look. In the meantime this is my work around. Tested working empirically with jest 1.3.1 :) Just need to use the done param

beforeAll(async (done) => {
    const accessToken = await api.getAccessToken()
    // Do whatever you need to do
    done()
})

@dyst5422
Copy link

dyst5422 commented Mar 1, 2018

Can we reopen this one as beforeAll, afterAll was not addressed?

@tonyxiao
Copy link
Author

tonyxiao commented Mar 2, 2018

@dyst5422 it's working for me no.

@SimenB
Copy link
Member

SimenB commented Mar 2, 2018

@dyst5422
Copy link

dyst5422 commented Mar 2, 2018

Yeah, I realized this later today. My issue was in trying to asyncronously control which tests ran. So i was doing it.skipIf(() => Promise, 'mytest', () => Promise), which clearly won't work because jest collects all tests syncronously.

@SimenB
Copy link
Member

SimenB commented Mar 2, 2018

You can track #5673. Not sure if it covers your use case, though

@CMCDragonkai
Copy link

Why do we need to call done if the async function already returns a promise when called?

@thymikee
Copy link
Collaborator

@CMCDragonkai if you use async/await/promises you don't need to call done.

@CMCDragonkai
Copy link

CMCDragonkai commented Jun 16, 2019

I tested it here:

import { Client } from 'pg';

let db;

beforeAll(async (done) => {
  db = new Client();
  await db.connect();
  done();
});

afterAll(async (done) => {
  await db.end();
  done();
});

The above works, but if I remove the done() from them, they result in an error.

Oh I just tested again without the done parameter. It ends up working awesome!

@neaumusic
Copy link

The "accepted" comment makes no sense, jest shouldn't be analyzing function innards to see if done is actually used or not. That function thread shouldn't terminate until await resolves and the function implicitly returns undefined on the next line

@VitorBrangioni
Copy link

Same issue, any solution?

@fider-apptimia
Copy link

@VitorBrangioni
I met this issue when using "module": "es2015" (tsconfig.json).
This causes jest to run lifecycle hooks without wait.

To ensure lifecycle hooks execution order I am using "module": "commonJS" (tsconfig.json).
Btw. I have no idea about browser code testing, for Node.js it fixes issue.

@arekbal
Copy link

arekbal commented Jun 8, 2020

Just experienced it.
Have "module": "commonJS" and everything else properly set (I believe).
In my case, beforeEach was timing out without visible error and jest started running tests. It can easily be replicated by setting low timeout and some delay promise inside beforeEach. It leads to no error.

It looks like a bug.

I workarounded it by setting custom high timeout value.
If I will need timeout there I will have to implement it myself.

Edit: Actually it skips other errors as well.

@devniel
Copy link

devniel commented Sep 9, 2020

I'm getting something similar, errors from async ops are getting ignored, the only way to get them is by using try/catch.

beforeEach(async () => {
   await getConnection().synchronize(true); // throws error
   log('done'); // this isn't printed
});

@LucaColonnello
Copy link

Same as @devniel said here,
if I don't use done and any part of the call stack throws, the error is not propagated.

Even using the done callback, it seems like in case of errors, the test spec is not reporting and is moving on the it statements anyway...

@SimenB
Copy link
Member

SimenB commented Nov 1, 2020

This issue is closed. Any bugs with the current release of Jest (v26 at the time of writing) should be reported in new issues with reproductions.

In general though, using both a done function and returning a promise is weird and might throw in the future. It's non-obvious what the user wants Jets to wait for in that case. You should use only one of the 2 ways of marking a test or hooks async (personally I prefer promises, but that's up to you)

@Xample
Copy link

Xample commented Nov 2, 2020

I don't know where is the catch but using an async and / or using the callback NOT prevents other it('')code to be run in parallel. Something is not reliable

in short: this simplified code can fail (variable is undefined)

  let variable;
  beforeAll(async (done) => {
    variable = await initVariable();
    done();
  });

  it('should do something', async () => {
  expect(variable).toBeDefined();
  }

this "can" fail as well (variable is undefined)

  let variable;
  beforeAll(async () => {
    variable = await initVariable();
  });

  it('should do something', async () => {
  expect(variable).toBeDefined();
  }

@ingun37
Copy link

ingun37 commented Nov 26, 2020

@Xample I thought I had the same problem but it turned out that I did not set the enough timeout for beforeAll. Please check if it’s not the case.

@foray1010
Copy link

@Xample I have the same issue, using jest-circus fixed it
See #8688 (comment)

@github-actions
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 May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.