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

Merged
merged 4 commits into from
Feb 17, 2025
Merged

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 {
  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()
})

Bug fix

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

Tasks

  • Add docs

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 Feb 16, 2025 9:47pm

@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: 64f6635

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.

Files not reviewed (1)
  • packages/nuqs/package.json: Language not supported
Comments suppressed due to low confidence (3)

packages/nuqs/src/testing.ts:25

  • [nitpick] The error message could be more clear and helpful. Consider rephrasing to: '[nuqs] testSerializeThenParse: The parsed value is null when parsing the serialized value ${serialized} from the input ${input}.'
[nuqs] testSerializeThenParse: parsed value is null (when parsing ${serialized} serialized from ${input})

packages/nuqs/src/testing.ts:62

  • [nitpick] The error message could be more clear and helpful. Consider rephrasing to: '[nuqs] testParseThenSerialize: The parsed value is null when parsing the input ${input}.'
[nuqs] testParseThenSerialize: parsed value is null (when parsing ${input})

packages/nuqs/src/testing.ts:14

  • There is a typo in the usage example. 'testSerializeThanParse' should be 'testSerializeThenParse'.
expect(() => testSerializeThanParse(myParser, 'bar')).toThrow()
@franky47 franky47 force-pushed the feat/testing-custom-parsers branch from 3963c53 to 0872894 Compare January 16, 2025 14:20
@franky47 franky47 force-pushed the feat/testing-custom-parsers branch from 0872894 to 177ecb3 Compare January 23, 2025 21:02
@franky47 franky47 force-pushed the feat/testing-custom-parsers branch from 177ecb3 to 7064274 Compare January 31, 2025 10:02
@franky47 franky47 force-pushed the feat/testing-custom-parsers branch from 7064274 to 2c81c18 Compare February 1, 2025 12:43
@franky47 franky47 force-pushed the feat/testing-custom-parsers branch from 2c81c18 to eba5ade Compare February 7, 2025 11:32
@franky47 franky47 force-pushed the feat/testing-custom-parsers branch from eba5ade to c40da19 Compare February 13, 2025 09:31
@franky47 franky47 force-pushed the feat/testing-custom-parsers branch from c40da19 to bf519bf Compare February 13, 2025 15:58
@franky47 franky47 force-pushed the feat/testing-custom-parsers branch from bf519bf to e1d827e Compare February 13, 2025 19:17
@franky47 franky47 force-pushed the feat/testing-custom-parsers branch from d89e57b to 64f6635 Compare February 16, 2025 21:42
@franky47 franky47 merged commit 08a5752 into next Feb 17, 2025
28 checks passed
@franky47 franky47 deleted the feat/testing-custom-parsers branch February 17, 2025 09:42
Copy link

🎉 This PR is included in version 2.4.0-beta.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link

🎉 This PR is included in version 2.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@franky47 franky47 removed this from the 🚀 Shipping next milestone Feb 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant