refactor(mock): emit strict mock types once from structured names - #3543
Conversation
generateFakerForSchemas returns strictMockSchemaTypeNames instead of inlining helpers and aliases; writeFakerSchemaMocks finalizes like operation mocks. Remove regex scraping from dedupeStrictMockTypeDeclarations. Closes #3542 Co-authored-by: Cursor <cursoragent@cursor.com>
📝 WalkthroughWalkthroughgenerateFakerForSchemas no longer inlines strict-mock helper/type declarations; it returns an optional ChangesStrict Mock Schema Type Name Extraction
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/mock/src/faker/index.ts (1)
168-170:⚠️ Potential issue | 🟠 Major | ⚡ Quick winTrack strict names for every strict schema factory.
Line 168 only records
typeNamefor overridable schemas, butgetMockFactorySignatureParts()inpackages/mock/src/mock-types.tsstill returns{Schema}Mockfor strict non-overridable factories. That means a top-level array/scalar/enum schema can generategetFooMock(): FooMockwhilewriteFakerSchemaMocks()never receivesFooinstrictSchemaTypeNames, so the finalizer will skipexport type FooMock = …and the emitted file stops typechecking.Suggested fix
- if (isStrictMock(mockOptions) && isOverridable) { + if (isStrictMock(mockOptions)) { strictMockTypeNames.add(typeName); }Please add a regression test for a strict non-object schema when fixing this.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/mock/src/faker/index.ts` around lines 168 - 170, The code only adds typeName to strictMockTypeNames when isOverridable is true, causing non-overridable strict schemas to be omitted; update the logic in packages/mock/src/faker/index.ts (around isStrictMock and isOverridable) to always add typeName to strictMockTypeNames when isStrictMock(mockOptions) is true, regardless of isOverridable, so writeFakerSchemaMocks and strictSchemaTypeNames receive the strict names expected by getMockFactorySignatureParts (which returns {Schema}Mock for strict non-overridable factories); after fixing, add a regression test that creates a strict non-object schema (e.g., top-level enum/array/scalar) and asserts the emitted file includes the corresponding exported type alias (e.g., FooMock) so the emitted types typecheck.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@packages/mock/src/faker/index.ts`:
- Around line 168-170: The code only adds typeName to strictMockTypeNames when
isOverridable is true, causing non-overridable strict schemas to be omitted;
update the logic in packages/mock/src/faker/index.ts (around isStrictMock and
isOverridable) to always add typeName to strictMockTypeNames when
isStrictMock(mockOptions) is true, regardless of isOverridable, so
writeFakerSchemaMocks and strictSchemaTypeNames receive the strict names
expected by getMockFactorySignatureParts (which returns {Schema}Mock for strict
non-overridable factories); after fixing, add a regression test that creates a
strict non-object schema (e.g., top-level enum/array/scalar) and asserts the
emitted file includes the corresponding exported type alias (e.g., FooMock) so
the emitted types typecheck.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d0b9435d-6b6a-4cf3-ae6d-12a08d809da1
📒 Files selected for processing (6)
packages/mock/src/faker/index.test.tspackages/mock/src/faker/index.tspackages/mock/src/index.tspackages/mock/src/mock-types.test.tspackages/mock/src/mock-types.tspackages/orval/src/write-specs.ts
💤 Files with no reviewable changes (1)
- packages/mock/src/index.ts
Co-authored-by: Cursor <cursoragent@cursor.com>
…ories
Strict enum/array/scalar schema factories return {Schema}Mock but were
omitted from strictMockSchemaTypeNames when not overridable.
Co-authored-by: Cursor <cursoragent@cursor.com>
|
@wadakatu We merged the previous PR. |
Review — LGTM ✅Beyond being a clean refactor, this actually fixes a latent TS2304 bug for non-overridable strict faker schemas. I regenerated and typechecked locally against a spec with top-level enum/array/scalar schemas. The one real behavioral change: dropping
|
Add Status, PetList, and Score schemas to the strict fixture so CI typechecks StatusMock/PetListMock/ScoreMock emission. Document the once-per-file finalize invariant and simplify prepend logic. Co-authored-by: Cursor <cursoragent@cursor.com>
|
@wadakatu I summon you again :D |
wadakatu
left a comment
There was a problem hiding this comment.
All three are addressed cleanly — thanks for the quick turnaround! 🙌
Verified locally on the latest commit:
- Suggestion 1 (fixture): regenerated
issue-3525and the newStatus/PetList/Scoreschemas now emitStatusMock/PetListMock/ScoreMockand typecheck (incl. the odd-but-validScoreMockovernumber). The non-overridable strict-faker path is now locked down by both the snapshot andtypecheck-generatedgates. - Suggestion 2 (invariant): the "not idempotent / call once per file" note matches how the writers and
writeFakerSchemaMocksuse it. 👍 - Suggestion 3 (cleanup):
${header}\n\n${implementation.trimStart()}is output-equivalent (headeris always non-empty once we're past the empty-names early return, and.trimStart()subsumes the old.replace(/^\n+/, '')).
Full check: @orval/mock units (219) green, snapshot suite green after a fresh regen, and tsc --noEmit over all of generated/mock is clean — no output changes anywhere outside issue-3525, so no regressions.
LGTM 🚀
Summary
Follow-up refactor from strict mock types (#3525). Aligns the faker
schemas: truepath with MSW/operation mocks: generators only emit factories, andfinalizeMockImplementationprepends strict helpers and{Schema}Mockaliases once fromstrictMockSchemaTypeNames.generateFakerForSchemasreturnsstrictMockSchemaTypeNamesand no longer inlines strict type blockswriteFakerSchemaMockscallsfinalizeMockImplementation(same as other writers)dedupeStrictMockTypeDeclarationsonly prepends from the structured set (removed regex scraping/strip logic)Closes #3542
Test plan
@orval/mockunit testsorval-tests build(mock client typecheck)Summary by CodeRabbit
Improvements
Bug Fixes
New Features
Tests