Repository navigation
feat(mock): support indexMockFiles in split mode - #3546
Conversation
In split mode, mock: { indexMockFiles: true } was silently ignored — only
tags-split honored it. Emit a dedicated index.<ext>.ts barrel (e.g.
index.msw.ts) per generator type that re-exports the single split-mode mock
file, so MSW mocks stay out of the models/production barrel and avoid the
jsdom WritableStream module-eval crash when a barrel re-exports .msw.
Closes orval-labs#3318
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR extends Orval's mock.indexMockFiles to work in split mode by emitting dedicated per-generator index barrels (e.g., index.msw.ts, index.faker.ts) that re-export mock artifacts, keeping mocks separate from production model barrels. ChangesSplit-mode mock barrel generation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 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.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds support for emitting dedicated mock barrel files (index.<ext>.ts) in split mode when mock.indexMockFiles is enabled, aligning behavior with tags-split and addressing #3318.
Changes:
- Implement
indexMockFilessupport inwriteSplitModeto generateindex.msw.ts/index.faker.tsbarrels in split mode. - Add unit + integration regression tests to assert mock barrels are written and returned for formatting hooks.
- Update docs and add new generated snapshots for the new default test fixture (
index-mock-file-split).
Reviewed changes
Copilot reviewed 33 out of 33 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/configs/default.config.ts | Adds a split-mode fixture enabling indexMockFiles with MSW+faker generators. |
| tests/api-generation.spec.ts | Adds integration regression test asserting index.msw.ts and index.faker.ts are emitted. |
| tests/snapshots/default/index-mock-file-split/model/pets.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/petWithTag.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/petCountry.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/petCallingCode.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/pet.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/listPetsSort.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/listPetsParams.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/labradoodleBreed.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/labradoodle.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/index.ts | Snapshot coverage for generated model barrel in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/error.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/dogType.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/dog.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/dachshundBreed.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/dachshund.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/createPetsSort.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/createPetsParams.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/createPetsBody.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/catType.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/model/cat.ts | Snapshot coverage for generated models in new fixture. |
| tests/snapshots/default/index-mock-file-split/index.msw.ts | Snapshot coverage for new split-mode MSW mock barrel. |
| tests/snapshots/default/index-mock-file-split/index.faker.ts | Snapshot coverage for new split-mode faker mock barrel. |
| tests/snapshots/default/index-mock-file-split/endpoints.ts | Snapshot coverage for generated client output in new fixture. |
| tests/snapshots/default/index-mock-file-split/endpoints.msw.ts | Snapshot coverage for generated MSW mocks in new fixture. |
| tests/snapshots/default/index-mock-file-split/endpoints.faker.ts | Snapshot coverage for generated faker mocks in new fixture. |
| packages/core/src/writers/split-mode.ts | Implements writing + returning index.<ext>.ts mock barrels in split mode. |
| packages/core/src/writers/split-mode.test.ts | Adds unit regression tests for indexMockFiles behavior in split mode. |
| packages/core/src/types.ts | Updates indexMockFiles inline documentation to include split mode. |
| docs/content/docs/reference/configuration/output.mdx | Documents indexMockFiles for split + tags-split. |
| docs/content/docs/guides/msw.mdx | Updates MSW guide to describe split-mode mock barrels + dynamic import rationale. |
| docs/content/docs/guides/faker.mdx | Updates faker guide to describe split-mode mock barrels. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (output.mock.indexMockFiles) { | ||
| for (const mockExtension of writtenMockExtensions) { | ||
| const indexMockPath = path.join( | ||
| dirname, | ||
| `index.${mockExtension}${extension}`, | ||
| ); | ||
| await writeGeneratedFile( | ||
| indexMockPath, | ||
| `export * from './${filename}.${mockExtension}'\n`, | ||
| ); | ||
| indexMockPaths.push(indexMockPath); | ||
| } |
There was a problem hiding this comment.
Good catch — fixed in 90183f5. The barrel now appends getImportExtension(output.fileExtension, output.tsconfig) (the same helper the models index.ts barrel uses in schemas.ts), so the specifier is extensionless for .ts and keeps .js for ESM/NodeNext outputs, matching the file on disk.
| @@ -172,6 +173,7 @@ export async function writeSplitMode({ | |||
| // suffix comes from `getMockFileExtensionByTypeName(entry)` (e.g. `.msw.ts` | |||
| // or `.faker.ts`). | |||
There was a problem hiding this comment.
Fixed in 90183f5 — reworded so it's clear getMockFileExtensionByTypeName(entry) returns msw/faker and the full filename suffix becomes .msw.ts / .faker.ts.
| it('returns and writes index.<ext>.ts re-exporting the mock file when enabled', async () => { | ||
| const target = path.join(tmpDir, 'petstore.ts'); | ||
| const props = { | ||
| ...createSplitModeProps(target), | ||
| output: createSplitModeOutput(target, { | ||
| mode: OutputMode.SPLIT, | ||
| mock: { | ||
| indexMockFiles: true, | ||
| generators: [{ type: OutputMockType.MSW }], | ||
| }, | ||
| }), | ||
| }; |
There was a problem hiding this comment.
Added in 90183f5 — a .js unit test in split-mode.test.ts that sets fileExtension: '.js' and asserts the emitted index.msw.js re-exports ./petstore.msw.js.
The index.<ext> barrel re-export was always extensionless, which breaks resolution for non-TS outputs (e.g. ESM .js, where it wrote petstore.msw.js but exported './petstore.msw'). Use getImportExtension — the same helper the models index barrel uses — so the specifier matches the file on disk (empty for .ts, '.js' for ESM/NodeNext). Adds a .js unit test and clarifies the mock-suffix comment. Addresses Copilot review on orval-labs#3546.
Goal
Make
mock: { indexMockFiles: true }work insplitmode, so MSW (and faker) mocks can be isolated into a dedicated barrel — just liketags-splitalready supports.Closes #3318
Problem
In
splitmode,indexMockFileswas silently ignored — only thetags-splitwriter honored it (split-tags-mode.ts), whilesplit-mode.tsnever read the flag. Confirmed onmaster(v8.15.0): generating withmode: 'split'+mock: { indexMockFiles: true }produced noindex.msw.ts.Because of that,
split-mode users have no supported way to keep MSW out of a barrel. When a hand-rolledindex.tsre-exports the generated.mswfile, every test that transitively imports that barrel crashes undervitest+jsdom:MSW v2's
sse.tsreferencesWritableStreamat module-evaluation time, andjsdomdoesn't provide that global (asetupFilespolyfill runs too late, after Vite has already evaluated the import chain). The only robust fix is to keep MSW in its own barrel that production/model barrels never re-export — exactly whatindexMockFilesprovides intags-split.Fix
packages/core/src/writers/split-mode.ts: after writing the mock files, whenindexMockFilesis enabled, emit one root-levelindex.<ext>.tsbarrel per generator type that re-exports the single split-mode mock file. The barrel paths are included in the writer's return value so they are formatted and passed toafterAllFilesWritehooks.Split mode keeps every mock of a given type in a single
<filename>.<ext>.tsfile, so the barrel re-exports it wholesale (export *) — there is no cross-file collision risk, and it matches the existingexport *shape thattags-splitalready uses for faker entries.Generated output (
mode: 'split',indexMockFiles: true)Scope is limited to
splitmode (the issue's explicit ask);tags-splitbehavior is unchanged.Tests
split-mode.test.ts): the barrel path is returned + written with the right re-export when enabled; nothing is emitted whenindexMockFiles: false.tests/configs/default.config.ts→index-mock-file-split, msw and faker): bothindex.msw.tsandindex.faker.tsare generated, exercising the per-extension loop. Models stay in their ownmodel/index.tsbarrel.Notes
indexMockFilesdefaults tofalse); no existing config combinessplit+indexMockFiles, so there is no snapshot churn outside the new fixture.output.mdx,guides/msw.mdx,guides/faker.mdx, and theOutputMocksConfigtype comment.Summary by CodeRabbit
New Features
Documentation
Tests