Skip to content

feat(mock): support indexMockFiles in split mode - #3546

Merged
melloware merged 2 commits into
orval-labs:masterfrom
wadakatu:fix/3318-split-index-mock-files
Jun 5, 2026
Merged

melloware merged 2 commits into
orval-labs:masterfrom
wadakatu:fix/3318-split-index-mock-files

Conversation

@wadakatu

@wadakatu wadakatu commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Goal

Make mock: { indexMockFiles: true } work in split mode, so MSW (and faker) mocks can be isolated into a dedicated barrel — just like tags-split already supports.

Closes #3318

Problem

In split mode, indexMockFiles was silently ignored — only the tags-split writer honored it (split-tags-mode.ts), while split-mode.ts never read the flag. Confirmed on master (v8.15.0): generating with mode: 'split' + mock: { indexMockFiles: true } produced no index.msw.ts.

Because of that, split-mode users have no supported way to keep MSW out of a barrel. When a hand-rolled index.ts re-exports the generated .msw file, every test that transitively imports that barrel crashes under vitest + jsdom:

ReferenceError: WritableStream is not defined
 ❯ node_modules/msw/src/core/sse.ts:787:39
 ❯ <generated>.msw.ts:11:1

MSW v2's sse.ts references WritableStream at module-evaluation time, and jsdom doesn't provide that global (a setupFiles polyfill 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 what indexMockFiles provides in tags-split.

Fix

packages/core/src/writers/split-mode.ts: after writing the mock files, when indexMockFiles is enabled, emit one root-level index.<ext>.ts barrel 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 to afterAllFilesWrite hooks.

Split mode keeps every mock of a given type in a single <filename>.<ext>.ts file, so the barrel re-exports it wholesale (export *) — there is no cross-file collision risk, and it matches the existing export * shape that tags-split already uses for faker entries.

Generated output (mode: 'split', indexMockFiles: true)

endpoints.ts        # API client
model/index.ts      # models barrel (no mocks)
index.msw.ts        # NEW — export * from './endpoints.msw'
index.faker.ts      # NEW (when a faker generator is configured)

Scope is limited to split mode (the issue's explicit ask); tags-split behavior is unchanged.

Tests

  • Unit (split-mode.test.ts): the barrel path is returned + written with the right re-export when enabled; nothing is emitted when indexMockFiles: false.
  • Snapshot + focused assertions (tests/configs/default.config.ts → index-mock-file-split, msw and faker): both index.msw.ts and index.faker.ts are generated, exercising the per-extension loop. Models stay in their own model/index.ts barrel.

Notes

  • Opt-in (indexMockFiles defaults to false); no existing config combines split + indexMockFiles, so there is no snapshot churn outside the new fixture.
  • Docs updated: output.mdx, guides/msw.mdx, guides/faker.mdx, and the OutputMocksConfig type comment.

Summary by CodeRabbit

  • New Features

    • Index mock barrel files are now emitted in split mode as well as tags-split, producing per-generator root-level mock entrypoints that re-export the generator-specific mock modules (preserving configured file extensions).
  • Documentation

    • Clarified mock.indexMockFiles behavior across all output modes and explained the generated root-level mock entrypoints.
  • Tests

    • Added regression tests validating index mock barrel generation in split mode.

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
Copilot AI review requested due to automatic review settings June 5, 2026 05:09
@coderabbitai

coderabbitai Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5f77902d-9409-42af-a03a-fb447199bab8

📥 Commits

Reviewing files that changed from the base of the PR and between 29ee2a1 and 90183f5.

📒 Files selected for processing (2)
  • packages/core/src/writers/split-mode.test.ts
  • packages/core/src/writers/split-mode.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/writers/split-mode.test.ts

📝 Walkthrough

Walkthrough

This 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.

Changes

Split-mode mock barrel generation

Layer / File(s) Summary
Documentation and type updates
docs/content/docs/guides/faker.mdx, docs/content/docs/guides/msw.mdx, docs/content/docs/reference/configuration/output.mdx, packages/core/src/types.ts
Guides and inline type docs updated to state that mock.indexMockFiles emits per-extension index.<ext>.ts barrels in both split and tags-split modes; split re-exports the single mock file, tags-split aggregates per-tag files.
Split-mode barrel generation logic
packages/core/src/writers/split-mode.ts
writeSplitMode now computes a per-generator mockExtension, writes per-generator mock files using .<mockExtension><extension>, tracks emitted mock extensions to avoid duplicates, and generates root-level index.<mockExtension><extension> barrels (using getImportExtension) when output.mock.indexMockFiles is enabled. Generated index barrels are appended to the returned path list.
Implementation regression tests
packages/core/src/writers/split-mode.test.ts
Tests added to assert that when mock.indexMockFiles: true in split mode, writeSplitMode returns and writes index.<ext>.ts barrels re-exporting the mock module; when false, no index barrels are produced; and that non-.ts extensions preserve output extensions.
Test configuration and validation
tests/configs/default.config.ts, tests/api-generation.spec.ts
New indexMockFileSplit test config (mode: 'split', mock.indexMockFiles enabled for msw and faker) and an integration test validate that generated index.msw.ts and index.faker.ts re-export their respective endpoints.* modules.
Generated snapshots for test scenario
tests/__snapshots__/default/index-mock-file-split/*
Orval-generated snapshot files for the indexMockFileSplit test case: endpoint clients, faker mock builders, MSW handlers, index.*.ts re-exports, and model files used to validate emitted barrels.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

  • #3318 — Resolves the split-mode MSW barrel import issue by emitting dedicated index mock barrels so MSW is not transitively loaded from production barrels.

Suggested labels

mock, msw

Suggested reviewers

  • melloware

Poem

🐰 Split mock barrels, separate and neat,
Mocks re-exported so tests can breathe,
Index files generated with care,
No MSW surprise at import time,
A tiny hop for cleaner tests.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Linked Issues check ❓ Inconclusive The PR addresses the primary objective from #3318 by implementing separate mock barrels (index.msw.ts, index.faker.ts) in split mode to prevent MSW from being re-exported into production barrels. However, #39 regarding swagger2openapi dependency bump is unrelated to the actual code changes. Verify whether #39 (swagger2openapi bump) is intentionally included or should be in a separate PR, as it is not reflected in the changeset but listed in PR objectives.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: enabling support for indexMockFiles in split mode, which directly aligns with the PR's primary objective and the changeset.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing indexMockFiles support in split mode: type updates, writer logic, tests, documentation, and test configurations. No unrelated changes detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 indexMockFiles support in writeSplitMode to generate index.msw.ts / index.faker.ts barrels 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.

Comment on lines +224 to +235
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);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread packages/core/src/writers/split-mode.ts Outdated
Comment on lines 172 to 174
@@ -172,6 +173,7 @@ export async function writeSplitMode({
// suffix comes from `getMockFileExtensionByTypeName(entry)` (e.g. `.msw.ts`
// or `.faker.ts`).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 90183f5 — reworded so it's clear getMockFileExtensionByTypeName(entry) returns msw/faker and the full filename suffix becomes .msw.ts / .faker.ts.

Comment on lines +78 to +89
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 }],
},
}),
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@melloware melloware added the msw MSW related issues label Jun 5, 2026
@melloware
melloware merged commit 7e48991 into orval-labs:master Jun 5, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

msw MSW related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

split mode: MSW barrel exports break vitest/jsdom — WritableStream is not defined at module import time

3 participants