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

NextJS with Jest and Nuqs #938

Open
f1yingbanana opened this issue Mar 16, 2025 · 2 comments
Open

NextJS with Jest and Nuqs #938

f1yingbanana opened this issue Mar 16, 2025 · 2 comments
Labels
adapters/testing Uses the testing adapter for mocking URL behaviours in tests

Comments

@f1yingbanana
Copy link

Context

What's your version of nuqs?

    "nuqs": "^2.4.1",

What framework are you using?

  • ✅ Next.js (app router)

Which version of your framework are you using?

    "next": "15.0.3",

Description

I am unable to test with nuqs without mocking in NextJS and Jest. NextJS has its own code transformer and Jest config wrapper, so it is difficult to follow the Nuqs/Jest guide and use experimental ESM support. Instead, I've been relying on NextJS config transpilePackages option to port the node packages back to CommonJS.

However, with the esm-only.cjs file, I'll get an exception in my tests. But even if I remove the manual exception, my test still cannot find the correct module. I suspect maybe #880 might fix this, but with the NextJS Jest wrapper over Jest config, it didn't work :(

Really want to not mock and wish jest supports ESM out of the box :( but meanwhile, are there any workarounds?

Reproduction

  1. Setup Jest in NextJS following NextJS testing guide
  2. Try to use Nuqs in a Jest test
  3. Fails :(
@f1yingbanana f1yingbanana added the bug Something isn't working label Mar 16, 2025
@franky47
Copy link
Member

Thanks for the report, I'll try and reproduce it, in the mean time, could you tell me what version of Node.js you're using please?

My recommendations would be:

  1. Use Vitest (very similar API to Jest, runs ESM natively and is way faster)
  2. If not possible, try the latest versions of Node.js which include the require(esm) feature for interoperability.

@franky47 franky47 added adapters/testing Uses the testing adapter for mocking URL behaviours in tests and removed bug Something isn't working labels Mar 16, 2025
@f1yingbanana
Copy link
Author

Yeah I was new to web-dev and it seems like I picked the wrong testing framework 🥲 I'll see if I can switch.

Meanwhile I'm on Node 22.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters/testing Uses the testing adapter for mocking URL behaviours in tests
Projects
None yet
Development

No branches or pull requests

2 participants