feat(mock): add override.mock.schemas for per-schema property overrides - #3645
Conversation
Property overrides (`override.mock.properties`) match by property name, so
an override for `color` applies to every schema that has a `color` property.
There was no way to give the same property name different mock values per
schema.
Add `override.mock.schemas`, keyed by schema name, each holding a `properties`
map with the same matching rules as `override.mock.properties` (bare name,
`/regex/`, exact `#.path`). It resolves between the tag- and global-property
tiers, so precedence is: operations > tags > schemas > properties.
Example:
override: {
mock: {
schemas: {
Apple: { properties: { color: () => faker.color.human() } },
Car: { properties: { color: () => 'midnight black' } },
},
},
}
When `schemas: true` is enabled, each `get<Schema>Mock` factory bakes its
schema-scoped override in, so references that delegate to the factory keep
the override — no inlining needed.
📝 WalkthroughWalkthroughAdds ChangesSchema-scoped mock overrides
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 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)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration. 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.
🧹 Nitpick comments (1)
packages/mock/src/faker/getters/scalar.test.ts (1)
1139-1168: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd the missing
tags > schemasprecedence assertion.This suite validates most of the new precedence contract, but it still lacks an explicit tag-scoped-over-schema-scoped test for this feature path.
Proposed test addition
describe('getMockScalar (schema-scoped overrides)', () => { @@ it('prefers an operation-scoped override over a schema-scoped one', () => { @@ expect(result.value).toBe("'op-color'"); }); + + it('prefers a tag-scoped override over a schema-scoped one', () => { + const result = getMockScalar({ + ...baseArg, + tags: ['vehicle'], + item: colorItem('Apple'), + mockOptions: { + tags: { vehicle: { properties: { color: "'tag-color'" } } }, + schemas: { Apple: { properties: { color: "'red'" } } }, + }, + context: scalarContext(), + }); + + expect(result.value).toBe("'tag-color'"); + }); it('prefers a schema-scoped override over a global property override', () => {🤖 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/getters/scalar.test.ts` around lines 1139 - 1168, The test suite for scalar mock overrides is missing a test case that validates the precedence rule where tag-scoped overrides should take precedence over schema-scoped overrides. Add a new test case after the existing precedence tests in the same file that follows the same pattern as the other tests, using getMockScalar with mockOptions containing both tags and schemas properties for the same scalar property, and assert that the tag-level override value is returned instead of the schema-level override value.
🤖 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.
Nitpick comments:
In `@packages/mock/src/faker/getters/scalar.test.ts`:
- Around line 1139-1168: The test suite for scalar mock overrides is missing a
test case that validates the precedence rule where tag-scoped overrides should
take precedence over schema-scoped overrides. Add a new test case after the
existing precedence tests in the same file that follows the same pattern as the
other tests, using getMockScalar with mockOptions containing both tags and
schemas properties for the same scalar property, and assert that the tag-level
override value is returned instead of the schema-level override value.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 41541cc9-f205-4d27-8626-1295097f09e4
📒 Files selected for processing (9)
docs/content/docs/guides/faker.mdxdocs/content/docs/reference/configuration/output.mdxpackages/core/src/types.tspackages/mock/src/faker/getters/scalar.test.tspackages/mock/src/faker/getters/scalar.tspackages/mock/src/faker/index.test.tspackages/mock/src/faker/resolvers/value.tspackages/mock/src/msw/mocks.test.tspackages/mock/src/msw/mocks.ts
|
Thanks for taking a peek @melloware. Do you have any concern or changes you'd like to request? P.S. We've really been loving the faker mocks <-> Orval at Turo! |
|
@jakiestfu this seems like a slick improvement to me! @wadakatu and @Hypenate any thoughts? |
|
Nice addition! Thanks |
Summary
override.mock.propertiesmatches by property name, so an override forcolorapplies to every schema that has acolorproperty. There was no way to give the same property name different mock values depending on which schema it belongs to.This adds
override.mock.schemas, keyed by schema name, so the same property name can mock differently per schema:getAppleMock()now mockscoloras a fruit color andgetCarMock()as a car color, even though both declarecolor: string.The keys under
propertiesuse the same matching rules asoverride.mock.properties— bare name,/regex/, or exact#.path.Precedence (first match wins):
override.operations→override.tags→override.mock.schemas→override.mock.properties.Generated output
Given two schemas that both have
color: string:With
schemas: true, a referencing schema keeps delegating to the factory and the override rides along:Changes
packages/core— addschemas?: Record<string, { properties }>toOverrideMockOptions(user-facing) andMockOptions(serialized form).packages/mock(msw/mocks.ts) —getMockWithoutFuncserializes function-valued schema overrides to IIFE strings, mirroring the existingoperations/tagshandling.packages/mock(faker/getters/scalar.ts) —getMockScalarresolves a schema-scoped tier keyed on the property's enclosing schema (item.parentName), between the tag and global-property tiers.override.mock.schemasreference entry.Notes
parentName). No change tohasOverrideTouchingSchemawas needed forschemas: true: eachget<Schema>Mockfactory is built with the same mock options, so delegation preserves the override (verified by test).fakerandmswgenerators.Tests
scalar.test.ts(override applied per schema; falls through when the schema name doesn't match; precedence vs operation/global).index.test.ts(different override per schema; preserved through factory delegation).mocks.test.ts(function → IIFE, value stringification, omitted when unset).Summary by CodeRabbit
New Features
Documentation