47ng / nuqs

Type-safe search params state manager for React frameworks - Like useState, but stored in the URL query string.
https://nuqs.47ng.com
MIT License
4.86k stars 104 forks source link

NuqsTestingAdapter using Jest throwing "this package is ESM only." #727

Closed GuilhermeMelow closed 3 weeks ago

GuilhermeMelow commented 3 weeks ago

Context

What's your version of nuqs?

-> 2.1.0

What framework are you using?

Which version of your framework are you using?

-> 14.2.1

Description

When I try to run tests and in this there are imports from 'nuqs/adapters/testing' or any 'nuqs/*', jest display me the error message: This package is ESM only. See https://github.com/47ng/nuqs/issues/691 for more details.. I already reviewed the issue, but didn't find anything that could help me resolve this problem.

Reproduction

https://codesandbox.io/p/live/7e72a9a1-a6f7-4696-83eb-5503e24fe781 Access the link, type pnpm test and press enter on terminal.

franky47 commented 3 weeks ago

Yeah, getting Jest to work well with ESM packages is a bit of a sport, even in 2024.

I managed to make it work locally using these steps (following the Jest ESM guide):

  1. Add the following options to your jest.config.ts file:
    extensionsToTreatAsEsm: [".ts", ".tsx"],
    transform: {},
  2. Change your test command in package.json to "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest" (adapt accordingly for Windows with cross-env)

Note: tested using Node.js 20.18.0 and 22.11.0 (LTS).

Edit: I managed to also make it work in CodeSandbox by doing those steps and switching "module" to "CommonJS" in tsconfig.json. For some reason that last step wasn't needed when testing locally.

GuilhermeMelow commented 3 weeks ago

Thanks for you help! It's worked in parts, now this is throwing errors about global jest definition like in the print: image

franky47 commented 3 weeks ago

That's a Jest-specific issue now, not much to do with nuqs. Glad it helped you going forward!

github-actions[bot] commented 1 week ago

:tada: This issue has been resolved in version 2.1.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: