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
5.02k stars 109 forks source link

Vitest storybook tests erroring since 1.13.2 #454

Closed sunwrobert closed 10 months ago

sunwrobert commented 10 months ago

Context

What's your version of nuqs? 1.13.2

-> Paste result from `cat package.json | grep -e nuqs -e next-usequerystate` here

Next.js information (obtained by running next info):

-> Paste result from `next info` here

Are you using:

Description

Ever since I upgraded to nuqs 1.13.2, I get the following error in my vitest tests that use nuqs through Storybook. 1.13.1 works perfectly fine

Error: [next-usequerystate] URL update rate-limited by the browser. Consider increasing `throttleMs` for keys %s. %O
web:test:unit:   See https://err.47ng.com/NUQS-429 market TypeError: Cannot read properties of undefined (reading 'router')

Reproduction

  1. Clone this repo: https://github.com/sunwrobert/nuqs-issue-demo
  2. npm install -> npm run test

The relevant test is nuqs-demo.test.tsx

Notice that the error above throws.

If I downgrade to 1.13.1, it doesn't throw.

franky47 commented 10 months ago

Thanks for the report. The only change between 1.13.1 and 1.13.2 concerns the pages router, but you mention using the app router.

Could you fill up the context section of the issue template please, so I can try and reproduce? Including your storybook/vitest setup would also be very useful, thanks.

This "router is undefined" error (ignore the rate-limit message, that is a try-catch being placed too wide) could indicate that Storybook doesn't run in a Next.js context, which is needed for nuqs to function.

sunwrobert commented 10 months ago

Sure, will get a repro

sunwrobert commented 10 months ago

@franky47 Added repro steps. I am using a custom mock router to get this to work, so the thing is that just that the error above throws in tests. I could try ignoring the error, but issue still stands that it'll cause my tests to fail in 1.13.2 but not 1.13.1

github-actions[bot] commented 10 months ago

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

The release is available on:

Your semantic-release bot :package::rocket:

franky47 commented 10 months ago

Thanks to your reproduction, I found the issue: nuqs tries to detect whether it's running in the pages router vs app router with a check that was introduced in 1.13.2, but this check always assumed we were running in Next.js and could access its internals, which are not set in test/mock environments.

Let me know if 1.15.2 works for you.

sunwrobert commented 10 months ago

Yup, it works. Thanks @franky47