Uh oh!
There was an error while loading. Please reload this page.
test: add tsd type tests for SemVerVersion/SemVerRange assignability - #304
Open
gomesalexandre wants to merge 2 commits into
Open
test: add tsd type tests for SemVerVersion/SemVerRange assignability#304gomesalexandre wants to merge 2 commits into
gomesalexandre wants to merge 2 commits into
Conversation
closesMetaMask#73 Adds src/versions.test-d.ts, following the existing .test-d.ts pattern (hex.test-d.ts, json.test-d.ts, caip-types.test-d.ts), proving the two opaque SemVer types are not mutually assignable at the type level even though both are strings at runtime, and that a plain string literal isn't assignable to either without going through its assertion function first.
yarn test:types (tsd) has been a silent no-op repo-wide since the package.json types field became ./dist/index.d.cts. tsd 0.29's auto-discovery does typingsFile.replace(/\.d\.ts$/, '.test-d.ts'), which doesn't match .d.cts, so the configured tsd.directory: "src" fallback never fires - instead globby matches the literal ./dist/index.d.cts path (which exists once CI builds before running test:types), finds one file with zero assertions in it, and tsd reports a clean pass on nothing. Reproduced: appending a bogus assertion to any existing *.test-d.ts file still exits 0 under the old script. Fixed by pointing tsd at the src glob explicitly, matching the tsd.directory config's original intent. Verified against all 5 existing test-d files (hex, json, caip-types, misc, versions): clean pass with the fix, and the same bogus-assertion mutation now correctly fails.
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 freeto 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.
closes#73
Adds
src/versions.test-d.ts, a tsd type-level test provingSemVerVersionandSemVerRange(opaque/branded string types) aren't mutually assignable to eachother or to a plain string, following the existing
.test-d.tspattern(
hex.test-d.ts,json.test-d.ts,caip-types.test-d.ts,misc.test-d.ts).Found while verifying:
yarn test:typeshas been a silent no-op repo-wideNot specific to this new file - every existing
.test-d.tsfile in the repo iscurrently unreachable by
yarn test:types. Root cause:tsd's auto-discoverydoes
typingsFile.replace(/\.d\.ts$/, '.test-d.ts')againstpackage.json'stypesfield, which is./dist/index.d.cts- the regex doesn't match.d.cts,so the fallback to the configured
"tsd": {"directory": "src"}glob never fires.Instead
globbymatches the literal (post-build)./dist/index.d.ctspath,finds one file containing zero assertions, and
tsdreports a clean pass onnothing.
One-line repro: append any bogus assertion to an existing
*.test-d.tsfile(e.g.
expectAssignable<Hex>(12345);inhex.test-d.ts) andyarn test:typesstill exits 0.
Fixed in the same PR by pointing the script at the src glob explicitly:
"test:types": "tsd --files 'src/**/*.test-d.ts'". Verified against all 5existing test-d files (hex, json, caip-types, misc, versions) - clean pass with
the fix, and the bogus-assertion mutation now correctly fails. Zero-risk to CI:
none of the 5 existing files have latent failures the broken discovery was
masking.
Happy to split this into two PRs (test file / script fix) if you'd prefer, but
shipping the test file alone would mean shipping code CI never actually runs, so
I bundled them.
Testing
Mutation-tested the new test file itself: inverted all 6 assertions
(
expectAssignable/expectNotAssignableswapped), confirmedtsdcatchesevery one with a precise brand-mismatch diagnostic, reverted, confirmed clean.
Full
jestsuite andtsc --noEmitunaffected (7 pre-existing unrelated errorsin
node_modules/web3-*typings, confirmed via stash comparison againstunmodified
main).eslintclean.Note
Low Risk
Test-only and script wiring changes with no runtime or public API impact.
Overview
Adds
src/versions.test-d.tsso CI checks thatSemVerVersionandSemVerRangestay distinct branded strings: values narrowed viaassertIsSemVerVersion/assertIsSemVerRangeassign to their own types, the two types are not interchangeable, and raw strings are not assignable without those assertions.Updates
test:typesfrom plaintsdtotsd --files 'src/**/*.test-d.ts'because discovery againstpackage.json’stypesfield (./dist/index.d.cts) never matched.test-d.ts, so type tests (including existinghex,json, etc.) were not actually executed.Reviewed by Cursor Bugbot for commit d6f39a9. Bugbot is set up for automated code reviews on this repo. Configure here.