Testing
Some tips on testing components that use `nuqs`
Since nuqs 2, you can unit-test components that use useQueryState(s)
hooks
by wrapping your rendered component in a NuqsTestingAdapter
.
With Vitest
Here is an example for Vitest and Testing Library to test a button rendering a counter:
See issue #259 for more testing-related discussions.
With Jest
Since nuqs 2 is an ESM-only package, there are a few hoops you need to jump through to make it work with Jest. This is extracted from the Jest ESM guide.
- Add the following options to your jest.config.ts file:
- Change your test command to include the
--experimental-vm-modules
flag:
Adapt accordingly for Windows with cross-env
.
Higher-order component
Creating a wrapper component to pass props to the NuqsTestingAdapter
can be verbose as your test suite grows.
You can use the withNuqsTestingAdapter
function to pass the right props,
and it will return a wrapper component: