Use this file to discover all available pages before exploring further.
Since nuqs 2, you can unit-test components that use 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.
The NuqsTestingAdapter{:ts} component provides a test environment for components
using nuqs hooks. It simulates the URL state management without requiring a real router.
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:
The initial search params to use for the test. These can be a query string, a URLSearchParams object or a record object with string values.
// As a query stringwithNuqsTestingAdapter({ searchParams: '?q=hello&limit=10'})// As URLSearchParamswithNuqsTestingAdapter({ searchParams: new URLSearchParams('?q=hello&limit=10')})// As an objectwithNuqsTestingAdapter({ searchParams: { q: 'hello', limit: '10' // Values are serialized strings }})
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), set hasMemory: true{:ts}, so subsequent updates build up on the previous state:
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 is true{:ts} by default to isolate tests, but you can set it to false{: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 to true{:ts}.
If you create custom parsers with createParser{:ts}, you will likely want to test them.Parsers should:
Define pure functions for parse{:ts}, serialize{:ts}, and eq{:ts}.
Be bijective: parse(serialize(x)) === x{:ts} and serialize(parse(x)) === x{:ts}.
To help test bijectivity, you can use helpers defined in nuqs/testing:
import { isParserBijective, testParseThenSerialize, testSerializeThenParse} from 'nuqs/testing'it('is bijective', () => { // Passing tests return true expect(isParserBijective(parseAsInteger, '42', 42)).toBe(true) // Failing test throws an error expect(() => isParserBijective(parseAsInteger, '42', 47)).toThrowError() // You can also test either side separately: expect(testParseThenSerialize(parseAsInteger, '42')).toBe(true) expect(testSerializeThenParse(parseAsInteger, 42)).toBe(true) // Those will also throw an error if the test fails, // which makes it easier to isolate which side failed: expect(() => testParseThenSerialize(parseAsInteger, 'not a number')).toThrowError() expect(() => testSerializeThenParse(parseAsInteger, NaN)).toThrowError()})
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.