fix(frontend): generate MSW mocks that satisfy their Zod schemas

This commit is contained in:
shira committed 2026-10-06 11:23:52 +03:00
1 parent ad4bb94426
commit 1aa505c743
18 files changed
+1439 -133

No files matched your search

+16 -4
View File
@@ -1,16 +1,18 @@
// Generates the OpenAPI 3.2 compatibility fixture with the production Orval
// settings into a temporary directory, type-checks the output with the
// frontend compiler settings, and checks that the API conventions reached the
// generated code. The directory is removed afterwards.
// settings and the mock pattern post-step into a temporary directory,
// type-checks the output with the frontend compiler settings, and checks that
// the API conventions reached the generated code. The directory is removed
// afterwards.
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { dirname, join, relative, resolve } from "node:path";
import { env, stdout } from "node:process";
import { env, execPath, stdout } from "node:process";
import { fileURLToPath } from "node:url";
const frontendRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const bin = (name) => resolve(frontendRoot, "node_modules/.bin", name);
const fixture = resolve(frontendRoot, "../api/testdata/openapi-3.2.0.yml");
// Text that each fixture convention leaves in the generated TypeScript.
const expectedFragments = {
@@ -22,6 +24,7 @@ const expectedFragments = {
Timestamp: "export const Timestamp = zod.iso.datetime(",
Problem: "export const Problem = zod.object(",
ProblemErrorType: "TError = ErrorType<Problem>",
PageCursorMock: 'fakeFromPattern("^[A-Za-z0-9_-]+$", {minLength: 1, maxLength: 512})',
HeaderArgument: "headers: CreateProbeItemHeaders",
OptionalHeaderArgument: "headers: UpdateProbeItemHeaders",
};
@@ -50,6 +53,14 @@ try {
run(bin("orval"), ["--config", "./codegen/orval.config.ts"], {
ORVAL_COMPAT_OUTPUT: outputDirectory,
});
// The same post-step as `pnpm run generate`, see codegen/mock-patterns.ts.
run(execPath, [
"./codegen/mock-patterns.ts",
"--spec",
fixture,
"--endpoints",
join(outputDirectory, "endpoints"),
]);
const files = typeScriptFiles(outputDirectory);
assert.ok(files.length > 0, "Orval generated no files for the compatibility fixture.");
@@ -66,6 +77,7 @@ try {
for (const [component, fragment] of Object.entries(expectedFragments)) {
assert.ok(generated.includes(fragment), `Generated code lacks ${component}: ${fragment}`);
}
assert.ok(!generated.includes("fromRegExp"), "Generated mocks still call faker fromRegExp.");
stdout.write(`Orval compatibility fixture: ${files.length} files type-checked.\n`);
} finally {
rmSync(outputDirectory, { recursive: true, force: true });