Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add custom parsers testing helpers #853

Draft
wants to merge 2 commits into
base: next
Choose a base branch
from

Conversation

franky47
Copy link
Member

@franky47 franky47 commented Jan 8, 2025

This PR introduces a new export, with two functions to help testing custom parsers.

Parsers must be bijective, which means:

  • On a valid input query string, running the parser then the serializer should output the same query string as the input
  • On a valid state, running the serializer then the parser should return an identical value.

For that second point, identical value means that the eq equality function of the parser should return true when fed the input and output.

Usage

Eg, in Vitest:

import { testSerializeThenParse, testParseThenSerialize } from 'nuqs/testing'

testSerializeThenParse(parseAsInteger, 123)
expect(() => testSerializeThanParse(parseAsInteger, 3.14)).toThrow() // Not bijective: loss of precision

testParseThenSerialize(parseAsInteger, '123')
expect(() => testParseThenSerialize(parseAsInteger, '3.14')).toThrow()

Bug fix

Also contains a bug fix for date parsers which were missing a suitable equality function.

Copy link

vercel bot commented Jan 8, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nuqs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 10, 2025 9:07am

@franky47 franky47 added this to the 🪵 Backlog milestone Jan 8, 2025
Copy link

pkg-pr-new bot commented Jan 8, 2025

Open in Stackblitz

npm i https://pkg.pr.new/nuqs@853

commit: 3963c53

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant