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

Possibility to write testEnvironment in esm #10018

Closed
kuka-radovan opened this issue May 11, 2020 · 14 comments
Closed

Possibility to write testEnvironment in esm #10018

kuka-radovan opened this issue May 11, 2020 · 14 comments

Comments

@kuka-radovan
Copy link

🚀 Feature Proposal

It would be awesome if there will be possibility to create testEnvironment as esm. It is actually possible only as cjs.

Motivation

I need to test API of my server written as esm. I want to start it in testEnvironment so I will need to import it.

Pitch

From Jest v. 25.4.0 there is support of esm, so I believe, this feature is meaningful from this perspective.

@SimenB
Copy link
Member

SimenB commented May 11, 2020

Thanks for opening a separate issue! We can probably do something similar to what we do for importing config files, catching the error and check if it's ERR_REQUIRE_ESM: https://github.com/facebook/jest/blob/0e0eeed9d794fe0d6d7e84f555d9684f9e944221/packages/jest-config/src/readConfigFileAndSetRootDir.ts#L26-L42

My one concern is how this affects e.g. #8751. Probably not a huge issue - your transformer should just leave it alone. And I don't see a use case for both ESM and transpiling (people should just transpile to CJS in that case).

@kuka-radovan
Copy link
Author

Your proposal seems reasonable. I don't see use case of ESM and code transpiling either.

@gilles-yvetot
Copy link
Contributor

@SimenB Thanks for all the work on Jest! Especially the transition to ESM!

Do you think this feature could land in v27 ?

@SimenB
Copy link
Member

SimenB commented Dec 16, 2020

This landed in #8751 and will be available in jest@27. You can test it via jest@next now

@SimenB SimenB closed this as completed Dec 16, 2020
@SimenB
Copy link
Member

SimenB commented Dec 16, 2020

Wait, no you mean native ESM! 😅

That's not done but should be quite straightforward if anybody wants to open a PR. Need to take the approach of #9573. Should probably attempt to transpile it first, then check the if an error thrown is ERR_REQUIRE_ESM and then use import

@SimenB SimenB reopened this Dec 16, 2020
@gilles-yvetot
Copy link
Contributor

Ahah yeah I meant native ESM. Let me take a look at the code and see if I am able to help

@gilles-yvetot
Copy link
Contributor

@SimenB I have never contributed yet to to open source projects so would you mind pointing to the right file for that.
To my understanding it is this one ?

@gilles-yvetot
Copy link
Contributor

Hey @SimenB, it took me a while to make this a priority. But here we are with my first open-source PR. I don't really know how to add test though

@gilles-yvetot
Copy link
Contributor

@SimenB checking in again to see how I can help move this forward

@kuka-radovan
Copy link
Author

@gilles-yvetot that's awesome someone has the same interest to introduce this feature. Unfortunately, I have no time to contribute, but I will appreciate, if I will be able to import and start my es6 server inside testEnvironment. 🤞 🤞 🤞

@gilles-yvetot
Copy link
Contributor

@kuka-radovan thanks! It is actually my first open-source PR and I am waiting for approval or advise on how to make it better

@SimenB
Copy link
Member

SimenB commented Apr 30, 2021

Fixed in #11033

@SimenB SimenB closed this as completed Apr 30, 2021
@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 31, 2021
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