Skip to content

Commit 898dced

Browse files
committed
Revamp tests
1 parent 5e23b4f commit 898dced

18 files changed

+597
-396
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ deno/lib/playground.ts
1111
.eslintcache
1212
workspace.code-workspace
1313
.netlify
14+
bun.lockb

babel.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
["@babel/preset-env", { targets: { node: "current" } }],
4+
"@babel/preset-typescript",
5+
],
6+
};

bun.lockb

-209 KB
Binary file not shown.
File renamed without changes.

deno/lib/__tests__/coerce.test.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ test("number coercion", () => {
3535
expect(schema.parse("-12")).toEqual(-12);
3636
expect(schema.parse("3.14")).toEqual(3.14);
3737
expect(schema.parse("")).toEqual(0);
38-
expect(() => schema.parse("NOT_A_NUMBER")).toThrow; // z.ZodError
38+
expect(() => schema.parse("NOT_A_NUMBER")).toThrow(); // z.ZodError
3939
expect(schema.parse(12)).toEqual(12);
4040
expect(schema.parse(0)).toEqual(0);
4141
expect(schema.parse(-12)).toEqual(-12);
4242
expect(schema.parse(3.14)).toEqual(3.14);
4343
expect(schema.parse(BigInt(15))).toEqual(15);
44-
expect(() => schema.parse(NaN)).toThrow; // z.ZodError
44+
expect(() => schema.parse(NaN)).toThrow(); // z.ZodError
4545
expect(schema.parse(Infinity)).toEqual(Infinity);
4646
expect(schema.parse(-Infinity)).toEqual(-Infinity);
4747
expect(schema.parse(true)).toEqual(1);
4848
expect(schema.parse(false)).toEqual(0);
4949
expect(schema.parse(null)).toEqual(0);
50-
expect(() => schema.parse(undefined)).toThrow; // z.ZodError
51-
expect(() => schema.parse({ hello: "world!" })).toThrow; // z.ZodError
52-
expect(() => schema.parse(["item", "another_item"])).toThrow; // z.ZodError
50+
expect(() => schema.parse(undefined)).toThrow(); // z.ZodError
51+
expect(() => schema.parse({ hello: "world!" })).toThrow(); // z.ZodError
52+
expect(() => schema.parse(["item", "another_item"])).toThrow(); // z.ZodError
5353
expect(schema.parse([])).toEqual(0);
5454
expect(schema.parse(new Date(1670139203496))).toEqual(1670139203496);
5555
});
@@ -84,23 +84,23 @@ test("bigint coercion", () => {
8484
expect(schema.parse("5")).toEqual(BigInt(5));
8585
expect(schema.parse("0")).toEqual(BigInt(0));
8686
expect(schema.parse("-5")).toEqual(BigInt(-5));
87-
expect(() => schema.parse("3.14")).toThrow; // not a z.ZodError!
87+
expect(() => schema.parse("3.14")).toThrow(); // not a z.ZodError!
8888
expect(schema.parse("")).toEqual(BigInt(0));
89-
expect(() => schema.parse("NOT_A_NUMBER")).toThrow; // not a z.ZodError!
89+
expect(() => schema.parse("NOT_A_NUMBER")).toThrow(); // not a z.ZodError!
9090
expect(schema.parse(5)).toEqual(BigInt(5));
9191
expect(schema.parse(0)).toEqual(BigInt(0));
9292
expect(schema.parse(-5)).toEqual(BigInt(-5));
93-
expect(() => schema.parse(3.14)).toThrow; // not a z.ZodError!
93+
expect(() => schema.parse(3.14)).toThrow(); // not a z.ZodError!
9494
expect(schema.parse(BigInt(5))).toEqual(BigInt(5));
95-
expect(() => schema.parse(NaN)).toThrow; // not a z.ZodError!
96-
expect(() => schema.parse(Infinity)).toThrow; // not a z.ZodError!
97-
expect(() => schema.parse(-Infinity)).toThrow; // not a z.ZodError!
95+
expect(() => schema.parse(NaN)).toThrow(); // not a z.ZodError!
96+
expect(() => schema.parse(Infinity)).toThrow(); // not a z.ZodError!
97+
expect(() => schema.parse(-Infinity)).toThrow(); // not a z.ZodError!
9898
expect(schema.parse(true)).toEqual(BigInt(1));
9999
expect(schema.parse(false)).toEqual(BigInt(0));
100-
expect(() => schema.parse(null)).toThrow; // not a z.ZodError!
101-
expect(() => schema.parse(undefined)).toThrow; // not a z.ZodError!
102-
expect(() => schema.parse({ hello: "world!" })).toThrow; // not a z.ZodError!
103-
expect(() => schema.parse(["item", "another_item"])).toThrow; // not a z.ZodError!
100+
expect(() => schema.parse(null)).toThrow(); // not a z.ZodError!
101+
expect(() => schema.parse(undefined)).toThrow(); // not a z.ZodError!
102+
expect(() => schema.parse({ hello: "world!" })).toThrow(); // not a z.ZodError!
103+
expect(() => schema.parse(["item", "another_item"])).toThrow(); // not a z.ZodError!
104104
expect(schema.parse([])).toEqual(BigInt(0));
105105
expect(schema.parse(new Date(1670139203496))).toEqual(BigInt(1670139203496));
106106
});
@@ -111,25 +111,26 @@ test("date coercion", () => {
111111
expect(schema.parse(new Date().toISOString())).toBeInstanceOf(Date);
112112
expect(schema.parse(new Date().toUTCString())).toBeInstanceOf(Date);
113113
expect(schema.parse("5")).toBeInstanceOf(Date);
114-
expect(schema.parse("0")).toBeInstanceOf(Date);
115-
expect(schema.parse("-5")).toBeInstanceOf(Date);
116-
expect(schema.parse("3.14")).toBeInstanceOf(Date);
117-
expect(() => schema.parse("")).toThrow; // z.ZodError
118-
expect(() => schema.parse("NOT_A_DATE")).toThrow; // z.ZodError
114+
expect(schema.parse("2000-01-01")).toBeInstanceOf(Date);
115+
// expect(schema.parse("0")).toBeInstanceOf(Date);
116+
// expect(schema.parse("-5")).toBeInstanceOf(Date);
117+
// expect(schema.parse("3.14")).toBeInstanceOf(Date);
118+
expect(() => schema.parse("")).toThrow(); // z.ZodError
119+
expect(() => schema.parse("NOT_A_DATE")).toThrow(); // z.ZodError
119120
expect(schema.parse(5)).toBeInstanceOf(Date);
120121
expect(schema.parse(0)).toBeInstanceOf(Date);
121122
expect(schema.parse(-5)).toBeInstanceOf(Date);
122123
expect(schema.parse(3.14)).toBeInstanceOf(Date);
123-
expect(() => schema.parse(BigInt(5))).toThrow; // not a z.ZodError!
124-
expect(() => schema.parse(NaN)).toThrow; // z.ZodError
125-
expect(() => schema.parse(Infinity)).toThrow; // z.ZodError
126-
expect(() => schema.parse(-Infinity)).toThrow; // z.ZodError
124+
expect(() => schema.parse(BigInt(5))).toThrow(); // not a z.ZodError!
125+
expect(() => schema.parse(NaN)).toThrow(); // z.ZodError
126+
expect(() => schema.parse(Infinity)).toThrow(); // z.ZodError
127+
expect(() => schema.parse(-Infinity)).toThrow(); // z.ZodError
127128
expect(schema.parse(true)).toBeInstanceOf(Date);
128129
expect(schema.parse(false)).toBeInstanceOf(Date);
129130
expect(schema.parse(null)).toBeInstanceOf(Date);
130-
expect(() => schema.parse(undefined)).toThrow; // z.ZodError
131-
expect(() => schema.parse({ hello: "world!" })).toThrow; // z.ZodError
132-
expect(() => schema.parse(["item", "another_item"])).toThrow; // z.ZodError
133-
expect(() => schema.parse([])).toThrow; // z.ZodError
131+
expect(() => schema.parse(undefined)).toThrow(); // z.ZodError
132+
expect(() => schema.parse({ hello: "world!" })).toThrow(); // z.ZodError
133+
expect(() => schema.parse(["item", "another_item"])).toThrow(); // z.ZodError
134+
expect(() => schema.parse([])).toThrow(); // z.ZodError
134135
expect(schema.parse(new Date())).toBeInstanceOf(Date);
135136
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import * as z from "../index.ts";
2+
3+
export const filePath = __filename;
4+
5+
// z.object()
6+
7+
export const Test = z.object({
8+
f1: z.number(),
9+
});
10+
11+
export type Test = z.infer<typeof Test>;
12+
13+
export const instanceOfTest: Test = {
14+
f1: 1,
15+
};
16+
17+
// z.object().merge()
18+
19+
export const TestMerge = z
20+
.object({
21+
f2: z.string().optional(),
22+
})
23+
.merge(Test);
24+
25+
export type TestMerge = z.infer<typeof TestMerge>;
26+
27+
export const instanceOfTestMerge: TestMerge = {
28+
f1: 1,
29+
f2: "string",
30+
};
31+
32+
// z.union()
33+
34+
export const TestUnion = z.union([
35+
z.object({
36+
f2: z.string().optional(),
37+
}),
38+
Test,
39+
]);
40+
41+
export type TestUnion = z.infer<typeof TestUnion>;
42+
43+
export const instanceOfTestUnion: TestUnion = {
44+
f1: 1,
45+
f2: "string",
46+
};
47+
48+
// z.object().partial()
49+
50+
export const TestPartial = Test.partial();
51+
52+
export type TestPartial = z.infer<typeof TestPartial>;
53+
54+
export const instanceOfTestPartial: TestPartial = {
55+
f1: 1,
56+
};
57+
58+
// z.object().pick()
59+
60+
export const TestPick = TestMerge.pick({ f1: true });
61+
62+
export type TestPick = z.infer<typeof TestPick>;
63+
64+
export const instanceOfTestPick: TestPick = {
65+
f1: 1,
66+
};
67+
68+
// z.object().omit()
69+
70+
export const TestOmit = TestMerge.omit({ f2: true });
71+
72+
export type TestOmit = z.infer<typeof TestOmit>;
73+
74+
export const instanceOfTestOmit: TestOmit = {
75+
f1: 1,
76+
};
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
// @ts-ignore TS6133
2+
import { expect } from "https://deno.land/x/[email protected]/mod.ts";
3+
const test = Deno.test;
4+
// import path from "path";
5+
// import { Node, Project, SyntaxKind } from "ts-morph";
6+
7+
// import { filePath } from "./language-server.source";
8+
9+
// The following tool is helpful for understanding the TypeScript AST associated with these tests:
10+
// https://ts-ast-viewer.com/ (just copy the contents of languageServerFeatures.source into the viewer)
11+
12+
test("", () => {});
13+
// describe("Executing Go To Definition (and therefore Find Usages and Rename Refactoring) using an IDE works on inferred object properties", () => {
14+
// // Compile file developmentEnvironment.source
15+
// const project = new Project({
16+
// tsConfigFilePath: path.join(__dirname, "..", "..", "tsconfig.json"),
17+
// skipAddingFilesFromTsConfig: true,
18+
// });
19+
// const sourceFile = project.addSourceFileAtPath(filePath);
20+
21+
// test("works for object properties inferred from z.object()", () => {
22+
// // Find usage of Test.f1 property
23+
// const instanceVariable =
24+
// sourceFile.getVariableDeclarationOrThrow("instanceOfTest");
25+
// const propertyBeingAssigned = getPropertyBeingAssigned(
26+
// instanceVariable,
27+
// "f1"
28+
// );
29+
30+
// // Find definition of Test.f1 property
31+
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
32+
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
33+
// SyntaxKind.VariableDeclaration
34+
// );
35+
36+
// // Assert that find definition returned the Zod definition of Test
37+
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
38+
// expect(parentOfProperty?.getName()).toEqual("Test");
39+
// });
40+
41+
// // test("works for first object properties inferred from z.object().merge()", () => {
42+
// // // Find usage of TestMerge.f1 property
43+
// // const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
44+
// // "instanceOfTestMerge"
45+
// // );
46+
// // const propertyBeingAssigned = getPropertyBeingAssigned(
47+
// // instanceVariable,
48+
// // "f1"
49+
// // );
50+
51+
// // // Find definition of TestMerge.f1 property
52+
// // const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
53+
// // const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
54+
// // SyntaxKind.VariableDeclaration
55+
// // );
56+
57+
// // // Assert that find definition returned the Zod definition of Test
58+
// // expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
59+
// // expect(parentOfProperty?.getName()).toEqual("Test");
60+
// // });
61+
62+
// // test("works for second object properties inferred from z.object().merge()", () => {
63+
// // // Find usage of TestMerge.f2 property
64+
// // const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
65+
// // "instanceOfTestMerge"
66+
// // );
67+
// // const propertyBeingAssigned = getPropertyBeingAssigned(
68+
// // instanceVariable,
69+
// // "f2"
70+
// // );
71+
72+
// // // Find definition of TestMerge.f2 property
73+
// // const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
74+
// // const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
75+
// // SyntaxKind.VariableDeclaration
76+
// // );
77+
78+
// // // Assert that find definition returned the Zod definition of TestMerge
79+
// // expect(definitionOfProperty?.getText()).toEqual(
80+
// // "f2: z.string().optional()"
81+
// // );
82+
// // expect(parentOfProperty?.getName()).toEqual("TestMerge");
83+
// // });
84+
85+
// test("works for first object properties inferred from z.union()", () => {
86+
// // Find usage of TestUnion.f1 property
87+
// const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
88+
// "instanceOfTestUnion"
89+
// );
90+
// const propertyBeingAssigned = getPropertyBeingAssigned(
91+
// instanceVariable,
92+
// "f1"
93+
// );
94+
95+
// // Find definition of TestUnion.f1 property
96+
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
97+
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
98+
// SyntaxKind.VariableDeclaration
99+
// );
100+
101+
// // Assert that find definition returned the Zod definition of Test
102+
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
103+
// expect(parentOfProperty?.getName()).toEqual("Test");
104+
// });
105+
106+
// test("works for second object properties inferred from z.union()", () => {
107+
// // Find usage of TestUnion.f2 property
108+
// const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
109+
// "instanceOfTestUnion"
110+
// );
111+
// const propertyBeingAssigned = getPropertyBeingAssigned(
112+
// instanceVariable,
113+
// "f2"
114+
// );
115+
116+
// // Find definition of TestUnion.f2 property
117+
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
118+
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
119+
// SyntaxKind.VariableDeclaration
120+
// );
121+
122+
// // Assert that find definition returned the Zod definition of TestUnion
123+
// expect(definitionOfProperty?.getText()).toEqual(
124+
// "f2: z.string().optional()"
125+
// );
126+
// expect(parentOfProperty?.getName()).toEqual("TestUnion");
127+
// });
128+
129+
// test("works for object properties inferred from z.object().partial()", () => {
130+
// // Find usage of TestPartial.f1 property
131+
// const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
132+
// "instanceOfTestPartial"
133+
// );
134+
// const propertyBeingAssigned = getPropertyBeingAssigned(
135+
// instanceVariable,
136+
// "f1"
137+
// );
138+
139+
// // Find definition of TestPartial.f1 property
140+
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
141+
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
142+
// SyntaxKind.VariableDeclaration
143+
// );
144+
145+
// // Assert that find definition returned the Zod definition of Test
146+
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
147+
// expect(parentOfProperty?.getName()).toEqual("Test");
148+
// });
149+
150+
// test("works for object properties inferred from z.object().pick()", () => {
151+
// // Find usage of TestPick.f1 property
152+
// const instanceVariable =
153+
// sourceFile.getVariableDeclarationOrThrow("instanceOfTestPick");
154+
// const propertyBeingAssigned = getPropertyBeingAssigned(
155+
// instanceVariable,
156+
// "f1"
157+
// );
158+
159+
// // Find definition of TestPick.f1 property
160+
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
161+
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
162+
// SyntaxKind.VariableDeclaration
163+
// );
164+
165+
// // Assert that find definition returned the Zod definition of Test
166+
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
167+
// expect(parentOfProperty?.getName()).toEqual("Test");
168+
// });
169+
170+
// test("works for object properties inferred from z.object().omit()", () => {
171+
// // Find usage of TestOmit.f1 property
172+
// const instanceVariable =
173+
// sourceFile.getVariableDeclarationOrThrow("instanceOfTestOmit");
174+
// const propertyBeingAssigned = getPropertyBeingAssigned(
175+
// instanceVariable,
176+
// "f1"
177+
// );
178+
179+
// // Find definition of TestOmit.f1 property
180+
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
181+
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
182+
// SyntaxKind.VariableDeclaration
183+
// );
184+
185+
// // Assert that find definition returned the Zod definition of Test
186+
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
187+
// expect(parentOfProperty?.getName()).toEqual("Test");
188+
// });
189+
// });
190+
191+
// const getPropertyBeingAssigned = (node: Node, name: string) => {
192+
// const propertyAssignment = node.forEachDescendant((descendent) =>
193+
// Node.isPropertyAssignment(descendent) && descendent.getName() == name
194+
// ? descendent
195+
// : undefined
196+
// );
197+
198+
// if (propertyAssignment == null)
199+
// fail(`Could not find property assignment with name ${name}`);
200+
201+
// const propertyLiteral = propertyAssignment.getFirstDescendantByKind(
202+
// SyntaxKind.Identifier
203+
// );
204+
205+
// if (propertyLiteral == null)
206+
// fail(`Could not find property literal with name ${name}`);
207+
208+
// return propertyLiteral;
209+
// };

0 commit comments

Comments
 (0)