Repository navigation
test: add type tests for the public API - #16
Merged
Merged
Conversation
Add src/__tests__/types.test-d.ts with expectTypeOf assertions for parseFormData (parameters, accepted inputs including native File values, return type), ParseFormDataOptions (removeEmptyString, depth, transformEntry signature) and the error classes. A transformEntry that returns a Date must not compile (issue #3). npm run typecheck checks the file. Exclude *.test-d.ts from vitest's runtime run: its include pattern matched the file, and at runtime every expectTypeOf call is a no-op, so the tests passed without checking anything.
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #16 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 4 4
Lines 134 134
Branches 45 45
=========================================
Hits 134 134 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
src/__tests__/types.test-d.ts:expectTypeOfassertions for the public API, checked bynpm run typecheck(tsconfig includessrc). No change tosrc/or the package;test:doesn't release.What is covered
parseFormDataIterable<[path: string, value: string | File]>, parameter 1 isParseFormDataOptions | undefinedFormData,URLSearchParams,[['a', 'x']], and entries with a nativeFilevaluenumberorBlobvalue (@ts-expect-error)FilesParseFormDataOptions: exact type (removeEmptyString,depth,transformEntry(entry, defaultTransform) => {path, value}), and atransformEntrythat callsdefaultTransformcompilestransformEntrythat returns aDatemust not compile (@ts-expect-error). This is the known limitation behind [Feature request]: Add date-string to date parsing flag #3; if a later change allows it, this test fails and has to be updated on purpose.DuplicateKeyError,MixedArrayError,ForbiddenKeyError,DepthLimitError,InvalidPathError): constructor takes[key: string], extendsError,key: string,name: stringThe expected types are written out in the test file instead of imported from
src/types.ts, so a change to the exported types fails the tests.Config change:
vitest.config.mtsvitest's
include(src/**/__tests__/**/*.ts) matched the new file and ran it as 13 runtime tests. At runtime everyexpectTypeOfcall is a no-op, so those tests always pass without checking anything. The file is now excluded (src/**/*.test-d.ts); vitest's typecheck mode isn't needed becausetscalready checks it.No other config changes needed:
*.test-d.tsas entry files, so the file isn't reported.src/**/__tests__/**is already excluded.expectTypeOfis already invitest/expect-expect'sassertFunctionNames.Checked that the tests can fail
Temporarily changed
src/(then restored) and rannpm run typecheck:depth?: number | stringinParseFormDataOptions→ the options test fails| Dateadded toJsonValue→ the return type test fails and theDate@ts-expect-errorbecomes unusedkey: string | numberinDuplicateKeyError→ itskeytest failsAlso, replacing the rejected
numbervalue with a string makes its@ts-expect-errorunused, so the rejection tests aren't passing for an unrelated reason.npm run validatepasses.