-
-
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
Use async-await with done does not work since v27. #11404
Comments
You need to use jest runner |
async-await doesn't work with done callbacks in Jest v27 Refs: jestjs/jest#11404
Override with // config-overrides.js
module.exports.jest = (config) => {
config.testRunner = 'jest-jasmine2';
return config;
}; |
Here is a workaround to allow a "done" function to be used with an async test. Change const functionUnderTest = (arg: any, callback: any) => { callback(arg); };
test("my async test using done", async (done) => {
const callback = jest.fn((result) => {
expect(result).toBe("expected value");
done();
});
const input = await Promise.resolve("expected value");
functionUnderTest(input, callback);
}); to test("my async test using done", async () => {
let done: () => void;
const callbackResolved = new Promise((resolve) => { done = resolve; });
const callback = jest.fn((result) => {
expect(result).toBe("expected value");
done();
});
const input = await Promise.resolve("expected value");
functionUnderTest(input, callback);
await callbackResolved;
}); You could also do the following but for a reason I don't understand this causes the test to also time out when the test fails. (When the test passes it's fine.) test("my async test using done", async () => {
await new Promise(async (done) => {
const callback = jest.fn(async (result) => {
expect(result).toBe("expected value");
done();
});
const input = await Promise.resolve("expected value");
functionUnderTest(input, callback);
});
}); |
Put in your
I had the issue with an angular project, solved it by setting the testRunner to jest-jasmine2 |
async-await doesn't work with done callbacks in Jest v27 Refs: jestjs/jest#11404
async-await doesn't work with done callbacks in Jest v27 Refs: jestjs/jest#11404
async-await doesn't work with done callbacks in Jest v27 Refs: jestjs/jest#11404
… Change Jest Library Behaviour Update from version 27 [jestjs/jest#11404]
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. |
Yeah, this is by design. You can use promises to handle anything the done callback does. |
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. |
💥 Regression Report
Last working version
26 (latest)
Stopped working in version:
27 (next)
Expected behavior
I just wonder why this is dropped since 27? Not sure if just the detection is new (never worked) or not.
But in some cases we may use both. The advantage of async-await and e.g. setTimeout. But since async transforms the return type from
void
toPromise<void>
Jest 27 detects it as returned value.I could wrap the
window.setTimeout
into aPromise
but what do you think about this issue?Here's a test demo.
I also wondering if Jest already supports an own sleep method like
jest.sleep(ms)
orjest.wait(ms)
orjest.delay(ms)
(Promise). Maybe a good feature request? Otherwise I could wrap the timeout like:But I would prefer a
jest.wait
function, instead of importing my own. But it's another issue.Jest Envinfo
The text was updated successfully, but these errors were encountered: