Is it supported by jest? Sometimes test setup methods do not run synchronously.
Also filed an issue in jest. https://github.com/jasmine/jasmine/issues/1145
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
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()
})
Can we reopen this one as beforeAll, afterAll was not addressed?
@dyst5422 it's working for me no.
All lifecycle functions accepts promises, yes: https://github.com/facebook/jest/blob/aef82a2a8ae9b249c40bcd558552bfb11ca43894/packages/jest-jasmine2/src/jasmine_async.js#L124-L127
Yeah, I realized this later today. My issue was in trying to asyncronously control which tests ran. So i was doing it.skipIf(() => Promise
You can track #5673. Not sure if it covers your use case, though
Why do we need to call done
if the async function already returns a promise when called?
@CMCDragonkai if you use async/await/promises you don't need to call done
.
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!
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
Same issue, any solution?
@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.
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.
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
});
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...
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)
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();
}
@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鈥檚 not the case.
Most helpful comment
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