Visitar URL original
fix: isolate esm async import bug by eryue0220 · Pull Request #14397 · jestjs/jest · GitHub
Skip to content

fix: isolate esm async import bug - #14397

Merged
SimenB merged 15 commits into
jestjs:mainfrom
eryue0220:fix/issue-14387
Aug 21, 2023
Merged

SimenB merged 15 commits into
jestjs:mainfrom
eryue0220:fix/issue-14387

Conversation

@eryue0220

@eryue0220 eryue0220 commented Aug 9, 2023 •

Copy link
Copy Markdown
Contributor

Summary

Fix: #14387

Test plan

test locally

 e2e/__tests__/asyncESMImport.test.ts
  ✓ runs test with async ESM import (282 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   1 passed, 1 total
Time:        0.514 s, estimated 1 s
Ran all test suites matching.

@netlify

netlify Bot commented Aug 9, 2023 •

Copy link
Copy Markdown

✅ Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit a06645e
🔍 Latest deploy log https://app.netlify.com/sites/jestjs/deploys/64e353c77896880008faac97
😎 Deploy Preview https://deploy-preview-14397--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@mrazauskas

Copy link
Copy Markdown
Contributor

Looks right, but the Test Plan went missing (;

Is it possible to add an e2e test? Hm.. As I was mentioning in the issue, there is no need to have "type": "module" in package.json. Simply await import() an .mjs file and run Jest with --experimental-vm-modules. That can be done like here:

test('runs test with native ESM', () => {
const {exitCode, stderr, stdout} = runJest('resolve-async', [], {
nodeOptions: '--experimental-vm-modules --no-warnings',
});

Or there is some reason, which I didn’t take into account?

@eryue0220

eryue0220 commented Aug 9, 2023 •

Copy link
Copy Markdown
Contributor Author

Looks right, but the Test Plan went missing (;

Is it possible to add an e2e test? Hm.. As I was mentioning in the issue, there is no need to have "type": "module" in package.json. Simply await import() an .mjs file and run Jest with --experimental-vm-modules. That can be done like here:

test('runs test with native ESM', () => {
const {exitCode, stderr, stdout} = runJest('resolve-async', [], {
nodeOptions: '--experimental-vm-modules --no-warnings',
});

Or there is some reason, which I didn’t take into account?

Sorry, I don't remember it. I'll fix them.

@eryue0220

Copy link
Copy Markdown
Contributor Author

Looks right, but the Test Plan went missing (;

Is it possible to add an e2e test? Hm.. As I was mentioning in the issue, there is no need to have "type": "module" in package.json. Simply await import() an .mjs file and run Jest with --experimental-vm-modules. That can be done like here:

test('runs test with native ESM', () => {
const {exitCode, stderr, stdout} = runJest('resolve-async', [], {
nodeOptions: '--experimental-vm-modules --no-warnings',
});

Or there is some reason, which I didn’t take into account?

I added test plan.

But the

@eryue0220 eryue0220 closed this Aug 9, 2023
@eryue0220 eryue0220 reopened this Aug 9, 2023
@eryue0220

Copy link
Copy Markdown
Contributor Author

I updated the test plan and the e2e test case.

Comment thread e2e/resolve-async/package.json Resolved

@mrazauskas mrazauskas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It is looking good.

I checked out and tried out on different versions of Node locally. Could you push the suggested changes to see if that also works in CI?

* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {jest} from '@jest/globals';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import {jest} from '@jest/globals';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import could not remove, because the next code will use the api jest. isolateModulesAsync

Comment thread e2e/async-esm-import/__tests__/test.js Resolved
Comment thread e2e/async-esm-import/__tests__/test.js Resolved

@mrazauskas mrazauskas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Seems like CI is happy.

One setup detail could be polished. I left an inline comment.

Comment thread e2e/async-esm-import/package.json Resolved
Comment thread e2e/async-esm-import/package.json Resolved
Comment thread packages/jest-runtime/src/index.ts Resolved

@mrazauskas mrazauskas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was going through again and notice few more details. Could you look at them, please?

Comment thread packages/jest-runtime/src/index.ts Outdated
Comment on lines +421 to +423
const registry = this._isolatedESMModuleRegistry
? this._isolatedESMModuleRegistry
: this._esmoduleRegistry;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm.. Isn’t it enough to use the same _isolatedMockRegistry? Did you try that?

It was my idea about missing _isolatedESMModuleRegistry, but it can I was wrong. Here we can see that _isolatedMockRegistry was simply not used while loading ES modules. Sorry about slow thinking. Could you try to _isolatedMockRegistry everywhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, it is possible. I originally thought it was intentionally differentiated because I saw that there was already an existing _isolatedModuleRegistry, moreover, I think it seems more appropriate.

Comment thread e2e/__tests__/asyncESMImport.test.ts Resolved
Comment thread e2e/async-esm-import/__tests__/main.js Resolved
Comment thread CHANGELOG.md Resolved

@mrazauskas mrazauskas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Simple and clear. Thanks for collaboration!

(CI fails are not related.)

@eryue0220

Copy link
Copy Markdown
Contributor Author

can this pr be merged, if no other problems ? cc @SimenB @mrazauskas

@SimenB SimenB left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonderful, thanks!

@SimenB
SimenB merged commit 58e8491 into jestjs:main Aug 21, 2023
@SimenB

SimenB commented Aug 21, 2023

Copy link
Copy Markdown
Member

@github-actions

Copy link
Copy Markdown

This pull request 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 Sep 21, 2023
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 this pull request may close these issues.

[Bug]: jest.isolateModulesAsync does not work with ESM modules

3 participants