Skip to content

Commit aef45b2

Browse files
committed
tests
1 parent 05e1939 commit aef45b2

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

packages/convex-helpers/server/zod4.functions.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,37 @@ export const transformAsync = zQuery({
182182
}),
183183
});
184184

185+
export const testQueryWithSkipConvexValidation = zQuery({
186+
args: {
187+
name: z.string(),
188+
age: z.number(),
189+
returnBadData: z.boolean().default(false),
190+
},
191+
handler: async (_ctx, args) => {
192+
assertType<{ name: string; age: number }>(args);
193+
const { name, age, returnBadData, ...rest } = args;
194+
if (Object.keys(rest).length > 0) {
195+
throw new Error("extraArg should be dropped");
196+
}
197+
if (returnBadData) {
198+
return {
199+
message: "bad data",
200+
doubledAge: 0n as any,
201+
};
202+
}
203+
return {
204+
message: `Hello ${name}, you are ${age} years old`,
205+
doubledAge: age * 2,
206+
extraArg: "extraArg",
207+
};
208+
},
209+
returns: z.object({
210+
message: z.string(),
211+
doubledAge: z.number(),
212+
}),
213+
skipConvexValidation: true,
214+
});
215+
185216
/**
186217
* Test codec in query args and return value
187218
*/
@@ -264,6 +295,7 @@ const testApi: ApiFromModules<{
264295
testQueryNoArgs: typeof testQueryNoArgs;
265296
testMutation: typeof testMutation;
266297
testAction: typeof testAction;
298+
testQueryWithSkipConvexValidation: typeof testQueryWithSkipConvexValidation;
267299
returnsNothing: typeof returnsNothing;
268300
transform: typeof transform;
269301
transformAsync: typeof transformAsync;
@@ -398,6 +430,39 @@ describe("zCustomQuery, zCustomMutation, zCustomAction", () => {
398430
});
399431
});
400432

433+
describe("skipConvexValidation", () => {
434+
test("zCustomQuery with skipConvexValidation", async () => {
435+
const t = convexTest(schema, modules);
436+
const args = {
437+
name: "Alice",
438+
age: 30,
439+
// Should get dropped
440+
extraArg: "extraArg",
441+
};
442+
const response = await t.query(
443+
testApi.testQueryWithSkipConvexValidation,
444+
args,
445+
);
446+
expect(response).toMatchObject({
447+
message: "Hello Alice, you are 30 years old",
448+
doubledAge: 60,
449+
});
450+
});
451+
test("throwing ConvexError on zod arg validation", async () => {
452+
const t = convexTest(schema, modules);
453+
await expect(
454+
t.query(testApi.testQueryWithSkipConvexValidation, {
455+
name: 123,
456+
} as any),
457+
).rejects.toThrowError(
458+
expect.objectContaining({
459+
data: expect.stringMatching(
460+
/(?=.*"ZodError")(?=.*"name")(?=.*"invalid_type")(?=.*"expected")(?=.*"string")/s,
461+
),
462+
}),
463+
);
464+
});
465+
});
401466
describe("transform", () => {
402467
test("calling a function with synchronous transforms in arguments and return values", async () => {
403468
const t = convexTest(schema, modules);

0 commit comments

Comments
 (0)