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

Use async-await with done does not work since v27. #11404

Closed
infacto opened this issue May 12, 2021 · 7 comments
Closed

Use async-await with done does not work since v27. #11404

infacto opened this issue May 12, 2021 · 7 comments

Comments

@infacto
Copy link

infacto commented May 12, 2021

💥 Regression Report

Test functions cannot both take a 'done' callback and return something. Either use a 'done' callback, or return a promise.

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 to Promise<void> Jest 27 detects it as returned value.

I could wrap the window.setTimeout into a Promise but what do you think about this issue?

Here's a test demo.

test('something', async (done) => {
  await something();
  setTimeout(() => {
    expect(another).toBe(42);
    done();
  }, 2000);
});

I also wondering if Jest already supports an own sleep method like jest.sleep(ms) or jest.wait(ms) or jest.delay(ms) (Promise). Maybe a good feature request? Otherwise I could wrap the timeout like:

export const wait = async (ms: number): Promise<void> => {
  return new Promise<void>((resolve) => {
    window.setTimeout(resolve, ms);
  });
}

But I would prefer a jest.wait function, instead of importing my own. But it's another issue.

Jest Envinfo

System:
  OS: Windows 10 10.0.19042
  CPU: (12) x64 Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
Binaries:
  Node: 14.15.1 - C:\Program Files\nodejs\node.EXE
  npm: 6.14.8 - C:\Program Files\nodejs\npm.CMD
npmPackages:
  jest: ^27.0.0-next.9 => 27.0.0-next.9
@ahnpnl
Copy link
Contributor

ahnpnl commented May 12, 2021

You need to use jest runner jest-jasmine2, v27 default uses jest-circus which doesn’t support done callback, see https://jestjs.io/blog/2020/05/05/jest-26 and #10529

trivikr added a commit to trivikr/aws-sdk-js-v3 that referenced this issue Oct 5, 2021
async-await doesn't work with done callbacks in Jest v27
Refs: jestjs/jest#11404
@ozluy
Copy link

ozluy commented Oct 22, 2021

Override with react-app-rewired worked for me

// config-overrides.js
module.exports.jest = (config) => {
  config.testRunner = 'jest-jasmine2';
 return config;
};

@dhull
Copy link

dhull commented Dec 16, 2021

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);
    });
  });

@spieIbug
Copy link

Put in your jest.config.js

module.exports = {
  // ... other settings,
  testRunner: 'jest-jasmine2' 
};

I had the issue with an angular project, solved it by setting the testRunner to jest-jasmine2

trivikr added a commit to trivikr/aws-sdk-js-v3 that referenced this issue Jan 4, 2022
async-await doesn't work with done callbacks in Jest v27
Refs: jestjs/jest#11404
trivikr added a commit to trivikr/aws-sdk-js-v3 that referenced this issue Jan 4, 2022
async-await doesn't work with done callbacks in Jest v27
Refs: jestjs/jest#11404
trivikr added a commit to trivikr/aws-sdk-js-v3 that referenced this issue Jan 4, 2022
async-await doesn't work with done callbacks in Jest v27
Refs: jestjs/jest#11404
ahmed-hamdy90 added a commit to ahmed-hamdy90/pluralsight-getting-started-github-actions-source that referenced this issue Apr 22, 2022
… Change Jest Library Behaviour Update from version 27 [jestjs/jest#11404]
@github-actions
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 31, 2022
@SimenB
Copy link
Member

SimenB commented Dec 31, 2022

Yeah, this is by design. You can use promises to handle anything the done callback does.

@SimenB SimenB closed this as not planned Won't fix, can't repro, duplicate, stale Dec 31, 2022
@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 Jan 31, 2023
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

6 participants