Skip to content

Commit ba86ebf

Browse files
committed
.
1 parent 7b7e815 commit ba86ebf

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

docs/mocks.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
MSW Auto-Mocker
22

3-
- Handlers: `src/mocks/handlers.ts` combines custom and auto-generated.
4-
- Custom: add overrides in `src/mocks/customHandlers/index.ts`.
5-
- Auto-gen: `src/mocks/mocker.ts` reads `swagger.json` and creates fixtures in `src/mocks/fixtures` on first run.
3+
- Handlers: `src/mocks/handlers.ts` combines non-schema mocks and auto-generated mocks.
4+
- Non-schema mocks: add hand-written handlers in `src/mocks/customHandlers/index.ts`. These run before auto-generated handlers so they can replace or extend behavior when needed.
5+
- Auto-generated: `src/mocks/mocker.ts` reads `swagger.json` and creates fixtures under `src/mocks/fixtures` on first run.
66
- Validation: Loaded fixtures are validated with Ajv; errors log to console.
77

88
Usage
9-
- Vitest: already wired via `src/mocks/test.setup.ts`. Run `pnpm test`.
10-
- Browser dev (optional): import and call `startWorker()` from `src/mocks/browser.ts` in your app's dev entry.
11-
- Standalone server (dev): run `pnpm mock:server` to start an HTTP mock server on `http://localhost:9090` (configurable via `MOCK_PORT`). Point your app's API base URL or a Next.js rewrite to this server to consume mock data without a real backend.
9+
- Vitest: tests initialize MSW in `src/mocks/test.setup.ts`. Run `pnpm test`.
10+
- Browser (optional): call `startWorker()` from `src/mocks/browser.ts` in your development entry point to mock requests in the browser.
11+
- Standalone server (dev): `pnpm mock:server` starts an HTTP mock server at `http://localhost:9090` (configurable with `MOCK_PORT`). Point API requests or Next.js rewrites to this origin to develop without a live backend.
1212

1313
Regeneration
14-
- Delete a fixture file or run with `AUTO_MOCKER_FORCE=1` to re-generate.
14+
- Delete a fixture file to re-generate it on next request.
1515

1616
Failure behavior (always strict)
1717
- If a schema is missing or faker fails, the handler responds 500 and does not write a placeholder.

src/mocks/customHandlers/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { RequestHandler } from "msw";
22

3-
// Add hand-written handlers here. These take precedence over
4-
// auto-generated ones because they are spread first in handlers.ts.
3+
// Add non-schema, hand-written mocks here.
4+
// These are composed before the auto-generated handlers (handlers.ts),
5+
// so they can replace or extend behavior where needed.
56
export const customHandlers: RequestHandler[] = [
67
// Example override: customize one endpoint's payload
78
// http.get("*/registry/v0.1/servers", () =>

src/mocks/fixtures/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Fixtures are auto-generated on first run by the MSW Auto-Mocker.
55
- You can edit these files to tailor responses for tests or local dev.
66

77
Notes
8-
- Custom handlers in `src/mocks/customHandlers` run before auto-generated ones.
9-
- To force re-generate a fixture, delete it or set env `AUTO_MOCKER_FORCE=1`.
8+
- Non-schema mocks in `src/mocks/customHandlers` run before auto-generated ones.
9+
- To re-generate a fixture, delete the file; it will be recreated on next request.
1010
- If your project exposes OpenAPI response types via `@api/types.gen`, you can
1111
enable type enforcement by setting `USE_TYPES_FOR_FIXTURES = true` in
1212
`src/mocks/mocker.ts`.

src/mocks/mocker.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ const USE_TYPES_FOR_FIXTURES = false; // set true if you have @api/types.gen ali
1717
// Strip these noisy prefixes from generated fixture folder names.
1818
const PREFIXES_TO_STRIP = ["api_v1beta_", "api_v0_"];
1919

20-
// Allow forcing regeneration of fixtures via env flag.
21-
const FORCE_REGENERATE = process.env.AUTO_MOCKER_FORCE === "1";
22-
2320
// ===== Runtime setup =====
2421
// Resolve module directory
2522
const __filename = fileURLToPath(import.meta.url);
@@ -420,7 +417,7 @@ export function autoGenerateHandlers() {
420417
}
421418

422419
const hasFile = fs.existsSync(fixtureFileName);
423-
if ((FORCE_REGENERATE || !hasFile) && successStatus !== "204") {
420+
if (!hasFile && successStatus !== "204") {
424421
let payload: unknown;
425422
if (successStatus) {
426423
const schema = getJsonSchemaFromOperation(

0 commit comments

Comments
 (0)