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

fix: isolate esm async import bug #14397

Merged
merged 15 commits into from
Aug 21, 2023
Merged

Conversation

eryue0220
Copy link
Contributor

@eryue0220 eryue0220 commented Aug 9, 2023

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
Copy link

netlify bot commented Aug 9, 2023

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
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
Copy link
Contributor Author

eryue0220 commented Aug 9, 2023

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
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
Contributor Author

I updated the test plan and the e2e test case.

Copy link
Contributor

@mrazauskas mrazauskas left a comment

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
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
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

e2e/async-esm-import/__tests__/test.js Outdated Show resolved Hide resolved
e2e/async-esm-import/__tests__/test.js Outdated Show resolved Hide resolved
Copy link
Contributor

@mrazauskas mrazauskas left a comment

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.

e2e/async-esm-import/package.json Outdated Show resolved Hide resolved
Copy link
Contributor

@mrazauskas mrazauskas left a comment

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 on lines 421 to 423
const registry = this._isolatedESMModuleRegistry
? this._isolatedESMModuleRegistry
: this._esmoduleRegistry;
Copy link
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
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.

e2e/__tests__/asyncESMImport.test.ts Outdated Show resolved Hide resolved
e2e/async-esm-import/__tests__/main.js Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Contributor

@mrazauskas mrazauskas left a comment

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
Contributor Author

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

Copy link
Member

@SimenB SimenB left a comment

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
Copy link
Member

SimenB commented Aug 21, 2023

@github-actions
Copy link

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