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

Challenges using nanoid under jest with jsdom environment? #439

Closed
cmdcolin opened this issue Aug 13, 2023 · 5 comments
Closed

Challenges using nanoid under jest with jsdom environment? #439

cmdcolin opened this issue Aug 13, 2023 · 5 comments

Comments

@cmdcolin
Copy link

cmdcolin commented Aug 13, 2023

Hi there

I am trying to use nanoid with jest with the jsdom environment, but this seems to import the specialized index.browser.js bundle, which uses pure-ESM statements, and then that causes challenges with jest when it's not configured for pure-ESM.

setting up a pure-ESM environment is hard, so I was hoping there might be a alternative solution. I would like to avoid custom jest configurations, because I want to use nanoid in a library, and don't really want my downstream users who may use jest to have to custom configure their jest either.

here is a small repro
https://github.com/cmdcolin/browser-nanoid-jest-repro

this issue happens with either nanoid v3 or v4 ( can change version in the repro package.json to see)

I know these bundle/esm issues are kind of tedious, if it is too specialized a use case, can close

@ai
Copy link
Owner

ai commented Aug 13, 2023

I recommend avoiding Jest because it too hacky and everything with ESM made it unusable. You can try uvu or vitest.

Another option is to use Jest option to change import path of nanoid and load Node.js version (but you need to use Nano ID 3).

@cmdcolin
Copy link
Author

Another option is to use Jest option to change import path of nanoid and load Node.js version (but you need to use Nano ID 3).

is this documented by chance?

@ai
Copy link
Owner

ai commented Aug 13, 2023

is this documented by chance?

No. It is a hacky and non-recommended way. The only non-hacky way is to migrate to ESM.

@ai
Copy link
Owner

ai commented Aug 13, 2023

Some even bigger hacks can be found here

#395

@cmdcolin
Copy link
Author

thank you for these examples! I will just close for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants