Skip to content

State setter functions not memoized when using NuqsTestingAdapter #1098

@georgwittberger

Description

@georgwittberger

Context

What's your version of nuqs?

2.5.1

What framework are you using?

  • ✅ Next.js (app router)
  • ❌ Next.js (pages router)
  • ❌ React SPA (no router)
  • ❌ Remix
  • ❌ React Router
  • ❌ Other (please specify)

Which version of your framework are you using?

15.5.2

Description

When using the NuqsTestingAdapter component in unit tests (Vitest, JSDOM) the setter functions returned by useQueryState are not memoized. The function changes on every render causing effects that depend on it to rerun more often than expected.

Reproduction

https://github.com/georgwittberger/nuqs-errors-repro

https://stackblitz.com/github/georgwittberger/nuqs-errors-repro

  1. Install dependencies, then run unit tests: npm run test
  2. See the failing tests in src/app/setters/page.test.tsx. These tests are expected to succeed.

The component renders a text indicating whether the setter function was exactly the same as in the previous render. In these unit tests the function identity changes.

However, in the browser with NuqsAdapter from nuqs/adapters/next/app this problem does not occur. The setter function is always the same there when the state changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    adapters/next/appUses the Next.js app routeradapters/testingUses the testing adapter for mocking URL behaviours in testsbugSomething isn't workingreleased

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions