|
| 1 | +import { build } from "esbuild"; |
| 2 | +import { createRequire } from "node:module"; |
| 3 | +import { dirname, resolve } from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | +import { describe, expect, it } from "vitest"; |
| 6 | + |
| 7 | +const require = createRequire(import.meta.url); |
| 8 | +const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../../.."); |
| 9 | + |
| 10 | +describe("schema composition compatibility", () => { |
| 11 | + it("parses Zod 4 parent schemas when the root Zod export resolves to Zod 3", async () => { |
| 12 | + const result = await build({ |
| 13 | + stdin: { |
| 14 | + contents: ` |
| 15 | + import { ScheduleMetadata, WebhookMetadata } from "./src/v3/schemas/schemas.ts"; |
| 16 | + import { WebhookResource } from "./src/v3/schemas/resources.ts"; |
| 17 | +
|
| 18 | + const verifierArtifact = { |
| 19 | + kind: "preset", |
| 20 | + preset: "stripe", |
| 21 | + config: { scheme: "shared-secret", placement: "header" }, |
| 22 | + }; |
| 23 | + const routingTarget = { type: "task", taskId: "my-task" }; |
| 24 | +
|
| 25 | + export const parsed = { |
| 26 | + schedule: ScheduleMetadata.parse({ |
| 27 | + cron: "0 0 * * *", |
| 28 | + timezone: "UTC", |
| 29 | + window: "10%", |
| 30 | + }), |
| 31 | + metadata: WebhookMetadata.parse({ |
| 32 | + id: "my-webhook", |
| 33 | + source: "stripe", |
| 34 | + verifierArtifact, |
| 35 | + routingTarget, |
| 36 | + }), |
| 37 | + resource: WebhookResource.parse({ |
| 38 | + id: "my-webhook", |
| 39 | + filePath: "src/trigger.ts", |
| 40 | + source: "stripe", |
| 41 | + verifierArtifact, |
| 42 | + routingTarget, |
| 43 | + }), |
| 44 | + }; |
| 45 | + `, |
| 46 | + loader: "ts", |
| 47 | + resolveDir: packageRoot, |
| 48 | + }, |
| 49 | + bundle: true, |
| 50 | + format: "esm", |
| 51 | + platform: "node", |
| 52 | + target: "node20", |
| 53 | + write: false, |
| 54 | + plugins: [ |
| 55 | + { |
| 56 | + name: "resolve-root-zod-to-v3", |
| 57 | + setup(build) { |
| 58 | + build.onResolve({ filter: /^zod$/ }, () => ({ path: require.resolve("zod/v3") })); |
| 59 | + }, |
| 60 | + }, |
| 61 | + ], |
| 62 | + }); |
| 63 | + |
| 64 | + const bundledModule = await import( |
| 65 | + `data:text/javascript;base64,${Buffer.from(result.outputFiles[0].text).toString("base64")}` |
| 66 | + ); |
| 67 | + |
| 68 | + expect(bundledModule.parsed).toMatchObject({ |
| 69 | + schedule: { window: "10%" }, |
| 70 | + metadata: { id: "my-webhook" }, |
| 71 | + resource: { id: "my-webhook" }, |
| 72 | + }); |
| 73 | + }); |
| 74 | +}); |
0 commit comments