NuqsTestingAdapter provides a test environment for components that use nuqs hooks. It simulates URL state management without requiring a real router, making it easy to test components in isolation.
NuqsTestingAdapter
A React component that provides a testing context for nuqs hooks.Props
Initial search params for the test. Can be provided as:
- A query string:
"?foo=bar&baz=qux" - A record object:
{ foo: "bar", baz: "qux" } - A URLSearchParams instance:
new URLSearchParams("foo=bar")
A callback function invoked whenever the URL is updated. Useful for asserting URL changes in tests.The function receives a
UrlUpdateEvent object containing:searchParams: The new search params as aURLSearchParamsinstancequeryString: The rendered query stringoptions: The options used for the URL update
Controls whether the adapter stores search params in memory and updates them on each URL change.
false(default): The adapter is immutable. Search params remain frozen to the initial value. This encourages testing discrete units of behavior.true: The adapter behaves like framework adapters, storing updates in memory so subsequent updates build on the previous state.
Whether to automatically reset the URL update queue after each update.In production, nuqs batches and throttles URL updates. This option controls whether the queue should be cleared after processing updates during tests.
Whether to clear the URL update queue when the adapter mounts.Since the update queue is a shared global, this option ensures test isolation by clearing the queue before each test. Set to
false to preserve the queue between renders for testing edge cases.Controls throttling behavior during tests.
0(default): No throttling - updates are immediate1: Production throttling (≥50ms)- Other values: Custom throttling factor
Default options to apply to all Supported options:
useQueryState hooks within this adapter.shallow: Whether to use shallow routing (framework-specific)scroll: Whether to scroll to top on URL updatesclearOnDefault: Whether to remove the key from URL when set to default valuelimitUrlUpdates: Whether to limit the rate of URL updates
A function to process or transform search params before they’re used by hooks.
The components to render within the testing adapter context.
withNuqsTestingAdapter
A higher-order component that creates a wrapper function for testing libraries that support wrapper components (e.g., Testing Library, Vitest).Signature
Parameters
Accepts allNuqsTestingAdapter props except children.
Returns
A wrapper component function that can be passed to testing library render methods.Usage
Types
UrlUpdateEvent
The event object passed to theonUrlUpdate callback.
The new search params as a
URLSearchParams instance. This is a copy that can be safely inspected or modified.The rendered query string representation of the search params (e.g.,
"?foo=bar&baz=qux").The options used for the URL update:
history:"push"|"replace"- The history method usedscroll:boolean- Whether to scroll to topshallow:boolean- Whether shallow routing was used
OnUrlUpdateFunction
The type signature for theonUrlUpdate callback function.
Complete Example
Here’s a complete example testing a counter component:counter.test.tsx
Direct Component Usage
You can also useNuqsTestingAdapter directly as a component:
See Also
- Testing Guide - Complete guide to testing with nuqs
- Custom Adapters - Creating custom adapters for other frameworks