forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.test.ts
57 lines (47 loc) · 1.51 KB
/
promise.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as z from '../index';
import { util } from '../helpers/util';
import { ZodError } from '../ZodError';
const promSchema = z.promise(
z.object({
name: z.string(),
age: z.number(),
}),
);
test('promise inference', () => {
type promSchemaType = z.infer<typeof promSchema>;
const t1: util.AssertEqual<promSchemaType, Promise<{ name: string; age: number }>> = true;
t1;
});
test('promise parsing success', () => {
promSchema.parse(Promise.resolve({ name: 'Bobby', age: 10 }));
});
test('promise parsing success 2', () => {
promSchema.parse({ then: () => {}, catch: () => {} });
});
test('promise parsing fail', () => {
const bad = promSchema.parse(Promise.resolve({ name: 'Bobby', age: '10' }));
expect(bad).rejects;
});
test('promise parsing fail 2', () => {
const failPromise = promSchema.parse(Promise.resolve({ name: 'Bobby', age: '10' }));
failPromise.catch(err => {
expect(err instanceof ZodError).toEqual(true);
});
});
test('promise parsing fail', () => {
const bad = () => promSchema.parse({ then: () => {}, catch: {} });
expect(bad).toThrow();
});
const asyncFunction = z.function(z.tuple([]), promSchema);
test('async function pass', () => {
const validatedFunction = asyncFunction.implement(async () => {
return { name: 'jimmy', age: 14 };
});
expect(validatedFunction()).resolves;
});
test('async function fail', () => {
const validatedFunction = asyncFunction.implement(() => {
return Promise.resolve('asdf' as any);
});
expect(validatedFunction()).rejects;
});