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

isolateModules not working for "await import('...')" in different tests #11709

Closed
aboe026 opened this issue Aug 1, 2021 · 3 comments
Closed

Comments

@aboe026
Copy link

aboe026 commented Aug 1, 2021

🐛 Bug Report

When calling jest.isolateModules in 2 different tests that each have await import('...') inside of them, the seconds seems effected by the execution of the first.

To Reproduce

Steps to reproduce the behavior:

  1. Pull down software-update-checker-cli
  2. Install dependencies: npm install
  3. Uncomment the calls main menu test in test/unit/index.unit.test.ts
  4. Run the 2 tests in the file: npm run test:unit -- -t 'index unit tests'

Observe the error:

$ npm run test:unit -- -t 'index unit tests'

> software-update-checker-cli@0.5.0 test:unit D:\Repos\software-update-checker-cli
> jest test/unit --collectCoverage --coverageDirectory=coverage/unit "-t" "index unit tests"

 FAIL  test/unit/index.unit.test.ts (7.23 s)
  ● Index Unit Tests › prints error and exits with unsuccessful code

    expect(received).toEqual(expected) // deep equality

    - Expected  - 3
    + Received  + 1

    - Array [
    -   Array [],
    - ]
    + Array []

      21 |     })
      22 |     await new Promise((resolve) => setTimeout(resolve, 0)) // need explicit sleep here because isolateModules does not await thrown error :/
    > 23 |     expect(homeSpy.mock.calls).toEqual([[]])
         |                                ^
      24 |     // Not sure why there are 2 errors/exists...
      25 |     expect(consoleErrorSpy.mock.calls).toEqual([[error], [colors.red(error)]])
      26 |     expect(exitSpy.mock.calls).toEqual([[1], [1]])

      at test/unit/index.unit.test.ts:23:32
      at step (test/unit/index.unit.test.ts:52:23)
      at Object.next (test/unit/index.unit.test.ts:33:53)
      at fulfilled (test/unit/index.unit.test.ts:24:58)

Summary of all failing tests
 FAIL  test/unit/index.unit.test.ts (7.23 s)
  ● Index Unit Tests › prints error and exits with unsuccessful code

    expect(received).toEqual(expected) // deep equality

    - Expected  - 3
    + Received  + 1

    - Array [
    -   Array [],
    - ]
    + Array []

      21 |     })
      22 |     await new Promise((resolve) => setTimeout(resolve, 0)) // need explicit sleep here because isolateModules does not await thrown error :/
    > 23 |     expect(homeSpy.mock.calls).toEqual([[]])
         |                                ^
      24 |     // Not sure why there are 2 errors/exists...
      25 |     expect(consoleErrorSpy.mock.calls).toEqual([[error], [colors.red(error)]])
      26 |     expect(exitSpy.mock.calls).toEqual([[1], [1]])

      at test/unit/index.unit.test.ts:23:32
      at step (test/unit/index.unit.test.ts:52:23)
      at Object.next (test/unit/index.unit.test.ts:33:53)
      at fulfilled (test/unit/index.unit.test.ts:24:58)


Test Suites: 1 failed, 24 skipped, 1 of 25 total
Tests:       1 failed, 333 skipped, 1 passed, 335 total
Snapshots:   0 total
Time:        8.45 s, estimated 11 s
Ran all test suites matching /test\\unit/i with tests matching "index unit tests".
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! software-update-checker-cli@0.5.0 test:unit: `jest test/unit --collectCoverage --coverageDirectory=coverage/unit "-t" "index unit tests"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the software-update-checker-cli@0.5.0 test:unit script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

With the first test commented out, the second test runs fine. It seems that the first test running prevents the ../../src/index import from running again (because the mainMenu spy does not run the second time (when it should run each time the ../../src/index import is called).

envinfo

System:
    OS: Windows 10 10.0.19042
    CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor
  Binaries:
    Node: 12.16.2 - C:\Program Files\nodejs\node.EXE
    npm: 6.14.4 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: 27.0.6 => 27.0.6
@noinkling
Copy link

isolatedModules doesn't support an async callback currently, see #10428

You might be able to work around it by getting a reference to the import() promise and awaiting it outside the callback?

@SimenB
Copy link
Member

SimenB commented Apr 25, 2022

yep, dupe of #10428

@SimenB SimenB closed this as completed Apr 25, 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 May 26, 2022
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

3 participants