useQueryState(s){:ts} hooks
without needing to mock anything, by using a dedicated testing adapter that will
facilitate setting up your tests (with initial search params) and asserting
on URL changes when acting on your components.
NuqsTestingAdapter
TheNuqsTestingAdapter{:ts} component provides a test environment for components
using nuqs hooks. It simulates the URL state management without requiring a real router.
Basic setup
Wrap your component under test with theNuqsTestingAdapter{:ts}:
Using withNuqsTestingAdapter
For testing libraries that support wrapper functions, usewithNuqsTestingAdapter{:ts}:
Testing with Vitest
Here is a complete example using Vitest and Testing Library:counter-button.test.tsx
Testing 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:
jest.config.ts
- Change your test command to include the
--experimental-vm-modulesflag:
package.json
Adapt accordingly for Windows with
cross-env.API Reference
searchParams
The initial search params to use for the test. These can be a query string, aURLSearchParams object or a record object with string values.
onUrlUpdate
A function that will be called when the URL is updated by the component. It receives an object with:searchParams{:ts}: the new search params as an instance ofURLSearchParams{:ts}queryString{:ts}: the new rendered query string (for convenience)options{:ts}: the options used to update the URL
hasMemory
By default, the testing adapter is immutable, meaning it will always use the initial search params as a base for URL updates. This encourages testing units of behaviour in a single test. To make it behave like framework adapters (which do store the updates in the URL), sethasMemory: true{:ts}, so subsequent updates build up on the previous state:
Advanced options
Internal/advanced options
Internal/advanced options
-
rateLimitFactor{:ts}: By default, rate limiting is disabled when testing, as it can lead to unexpected behaviours. Setting this to 1 will enable rate limiting with the same factor as in production. -
resetUrlUpdateQueueOnMount{:ts}: clear the URL update queue before running the test. This istrue{:ts}by default to isolate tests, but you can set it tofalse{:ts}to keep the URL update queue between renders and match the production behaviour more closely. -
autoResetQueueOnUpdate{:ts}: automatically reset the update queue after each URL update. Defaults totrue{:ts}.
Testing custom parsers
If you create custom parsers withcreateParser{:ts}, you will likely want to test them.
Parsers should:
- Define pure functions for
parse{:ts},serialize{:ts}, andeq{:ts}. - Be bijective:
parse(serialize(x)) === x{:ts}andserialize(parse(x)) === x{:ts}.
nuqs/testing:
Testing parser functions
The helper functions perform the following checks:isParserBijective(parser, serialized, input){:ts}
Tests that a parser is bijective by:
- Serializing the input and comparing to expected serialized value
- Parsing the serialized value and comparing to expected input value
- Using the parser’s
eq{:ts}function (if provided) for value comparison
testSerializeThenParse(parser, input){:ts}
Tests one direction: serialize the input, then parse it back and verify it matches the original input.
testParseThenSerialize(parser, serialized){:ts}
Tests the other direction: parse the serialized string, then serialize it back and verify it matches the original string.
Example: Testing a custom hex parser
hex-color.test.ts