Skip to content

Commit

Permalink
Revamp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhacks committed Jun 23, 2023
1 parent 5e23b4f commit 898dced
Show file tree
Hide file tree
Showing 18 changed files with 597 additions and 396 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ deno/lib/playground.ts
.eslintcache
workspace.code-workspace
.netlify
bun.lockb
6 changes: 6 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
};
Binary file removed bun.lockb
Binary file not shown.
File renamed without changes.
57 changes: 29 additions & 28 deletions deno/lib/__tests__/coerce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ test("number coercion", () => {
expect(schema.parse("-12")).toEqual(-12);
expect(schema.parse("3.14")).toEqual(3.14);
expect(schema.parse("")).toEqual(0);
expect(() => schema.parse("NOT_A_NUMBER")).toThrow; // z.ZodError
expect(() => schema.parse("NOT_A_NUMBER")).toThrow(); // z.ZodError
expect(schema.parse(12)).toEqual(12);
expect(schema.parse(0)).toEqual(0);
expect(schema.parse(-12)).toEqual(-12);
expect(schema.parse(3.14)).toEqual(3.14);
expect(schema.parse(BigInt(15))).toEqual(15);
expect(() => schema.parse(NaN)).toThrow; // z.ZodError
expect(() => schema.parse(NaN)).toThrow(); // z.ZodError
expect(schema.parse(Infinity)).toEqual(Infinity);
expect(schema.parse(-Infinity)).toEqual(-Infinity);
expect(schema.parse(true)).toEqual(1);
expect(schema.parse(false)).toEqual(0);
expect(schema.parse(null)).toEqual(0);
expect(() => schema.parse(undefined)).toThrow; // z.ZodError
expect(() => schema.parse({ hello: "world!" })).toThrow; // z.ZodError
expect(() => schema.parse(["item", "another_item"])).toThrow; // z.ZodError
expect(() => schema.parse(undefined)).toThrow(); // z.ZodError
expect(() => schema.parse({ hello: "world!" })).toThrow(); // z.ZodError
expect(() => schema.parse(["item", "another_item"])).toThrow(); // z.ZodError
expect(schema.parse([])).toEqual(0);
expect(schema.parse(new Date(1670139203496))).toEqual(1670139203496);
});
Expand Down Expand Up @@ -84,23 +84,23 @@ test("bigint coercion", () => {
expect(schema.parse("5")).toEqual(BigInt(5));
expect(schema.parse("0")).toEqual(BigInt(0));
expect(schema.parse("-5")).toEqual(BigInt(-5));
expect(() => schema.parse("3.14")).toThrow; // not a z.ZodError!
expect(() => schema.parse("3.14")).toThrow(); // not a z.ZodError!
expect(schema.parse("")).toEqual(BigInt(0));
expect(() => schema.parse("NOT_A_NUMBER")).toThrow; // not a z.ZodError!
expect(() => schema.parse("NOT_A_NUMBER")).toThrow(); // not a z.ZodError!
expect(schema.parse(5)).toEqual(BigInt(5));
expect(schema.parse(0)).toEqual(BigInt(0));
expect(schema.parse(-5)).toEqual(BigInt(-5));
expect(() => schema.parse(3.14)).toThrow; // not a z.ZodError!
expect(() => schema.parse(3.14)).toThrow(); // not a z.ZodError!
expect(schema.parse(BigInt(5))).toEqual(BigInt(5));
expect(() => schema.parse(NaN)).toThrow; // not a z.ZodError!
expect(() => schema.parse(Infinity)).toThrow; // not a z.ZodError!
expect(() => schema.parse(-Infinity)).toThrow; // not a z.ZodError!
expect(() => schema.parse(NaN)).toThrow(); // not a z.ZodError!
expect(() => schema.parse(Infinity)).toThrow(); // not a z.ZodError!
expect(() => schema.parse(-Infinity)).toThrow(); // not a z.ZodError!
expect(schema.parse(true)).toEqual(BigInt(1));
expect(schema.parse(false)).toEqual(BigInt(0));
expect(() => schema.parse(null)).toThrow; // not a z.ZodError!
expect(() => schema.parse(undefined)).toThrow; // not a z.ZodError!
expect(() => schema.parse({ hello: "world!" })).toThrow; // not a z.ZodError!
expect(() => schema.parse(["item", "another_item"])).toThrow; // not a z.ZodError!
expect(() => schema.parse(null)).toThrow(); // not a z.ZodError!
expect(() => schema.parse(undefined)).toThrow(); // not a z.ZodError!
expect(() => schema.parse({ hello: "world!" })).toThrow(); // not a z.ZodError!
expect(() => schema.parse(["item", "another_item"])).toThrow(); // not a z.ZodError!
expect(schema.parse([])).toEqual(BigInt(0));
expect(schema.parse(new Date(1670139203496))).toEqual(BigInt(1670139203496));
});
Expand All @@ -111,25 +111,26 @@ test("date coercion", () => {
expect(schema.parse(new Date().toISOString())).toBeInstanceOf(Date);
expect(schema.parse(new Date().toUTCString())).toBeInstanceOf(Date);
expect(schema.parse("5")).toBeInstanceOf(Date);
expect(schema.parse("0")).toBeInstanceOf(Date);
expect(schema.parse("-5")).toBeInstanceOf(Date);
expect(schema.parse("3.14")).toBeInstanceOf(Date);
expect(() => schema.parse("")).toThrow; // z.ZodError
expect(() => schema.parse("NOT_A_DATE")).toThrow; // z.ZodError
expect(schema.parse("2000-01-01")).toBeInstanceOf(Date);
// expect(schema.parse("0")).toBeInstanceOf(Date);
// expect(schema.parse("-5")).toBeInstanceOf(Date);
// expect(schema.parse("3.14")).toBeInstanceOf(Date);
expect(() => schema.parse("")).toThrow(); // z.ZodError
expect(() => schema.parse("NOT_A_DATE")).toThrow(); // z.ZodError
expect(schema.parse(5)).toBeInstanceOf(Date);
expect(schema.parse(0)).toBeInstanceOf(Date);
expect(schema.parse(-5)).toBeInstanceOf(Date);
expect(schema.parse(3.14)).toBeInstanceOf(Date);
expect(() => schema.parse(BigInt(5))).toThrow; // not a z.ZodError!
expect(() => schema.parse(NaN)).toThrow; // z.ZodError
expect(() => schema.parse(Infinity)).toThrow; // z.ZodError
expect(() => schema.parse(-Infinity)).toThrow; // z.ZodError
expect(() => schema.parse(BigInt(5))).toThrow(); // not a z.ZodError!
expect(() => schema.parse(NaN)).toThrow(); // z.ZodError
expect(() => schema.parse(Infinity)).toThrow(); // z.ZodError
expect(() => schema.parse(-Infinity)).toThrow(); // z.ZodError
expect(schema.parse(true)).toBeInstanceOf(Date);
expect(schema.parse(false)).toBeInstanceOf(Date);
expect(schema.parse(null)).toBeInstanceOf(Date);
expect(() => schema.parse(undefined)).toThrow; // z.ZodError
expect(() => schema.parse({ hello: "world!" })).toThrow; // z.ZodError
expect(() => schema.parse(["item", "another_item"])).toThrow; // z.ZodError
expect(() => schema.parse([])).toThrow; // z.ZodError
expect(() => schema.parse(undefined)).toThrow(); // z.ZodError
expect(() => schema.parse({ hello: "world!" })).toThrow(); // z.ZodError
expect(() => schema.parse(["item", "another_item"])).toThrow(); // z.ZodError
expect(() => schema.parse([])).toThrow(); // z.ZodError
expect(schema.parse(new Date())).toBeInstanceOf(Date);
});
76 changes: 76 additions & 0 deletions deno/lib/__tests__/language-server.source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import * as z from "../index.ts";

export const filePath = __filename;

// z.object()

export const Test = z.object({
f1: z.number(),
});

export type Test = z.infer<typeof Test>;

export const instanceOfTest: Test = {
f1: 1,
};

// z.object().merge()

export const TestMerge = z
.object({
f2: z.string().optional(),
})
.merge(Test);

export type TestMerge = z.infer<typeof TestMerge>;

export const instanceOfTestMerge: TestMerge = {
f1: 1,
f2: "string",
};

// z.union()

export const TestUnion = z.union([
z.object({
f2: z.string().optional(),
}),
Test,
]);

export type TestUnion = z.infer<typeof TestUnion>;

export const instanceOfTestUnion: TestUnion = {
f1: 1,
f2: "string",
};

// z.object().partial()

export const TestPartial = Test.partial();

export type TestPartial = z.infer<typeof TestPartial>;

export const instanceOfTestPartial: TestPartial = {
f1: 1,
};

// z.object().pick()

export const TestPick = TestMerge.pick({ f1: true });

export type TestPick = z.infer<typeof TestPick>;

export const instanceOfTestPick: TestPick = {
f1: 1,
};

// z.object().omit()

export const TestOmit = TestMerge.omit({ f2: true });

export type TestOmit = z.infer<typeof TestOmit>;

export const instanceOfTestOmit: TestOmit = {
f1: 1,
};
209 changes: 209 additions & 0 deletions deno/lib/__tests__/language-server.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// @ts-ignore TS6133
import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts";
const test = Deno.test;
// import path from "path";
// import { Node, Project, SyntaxKind } from "ts-morph";

// import { filePath } from "./language-server.source";

// The following tool is helpful for understanding the TypeScript AST associated with these tests:
// https://ts-ast-viewer.com/ (just copy the contents of languageServerFeatures.source into the viewer)

test("", () => {});
// describe("Executing Go To Definition (and therefore Find Usages and Rename Refactoring) using an IDE works on inferred object properties", () => {
// // Compile file developmentEnvironment.source
// const project = new Project({
// tsConfigFilePath: path.join(__dirname, "..", "..", "tsconfig.json"),
// skipAddingFilesFromTsConfig: true,
// });
// const sourceFile = project.addSourceFileAtPath(filePath);

// test("works for object properties inferred from z.object()", () => {
// // Find usage of Test.f1 property
// const instanceVariable =
// sourceFile.getVariableDeclarationOrThrow("instanceOfTest");
// const propertyBeingAssigned = getPropertyBeingAssigned(
// instanceVariable,
// "f1"
// );

// // Find definition of Test.f1 property
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
// SyntaxKind.VariableDeclaration
// );

// // Assert that find definition returned the Zod definition of Test
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
// expect(parentOfProperty?.getName()).toEqual("Test");
// });

// // test("works for first object properties inferred from z.object().merge()", () => {
// // // Find usage of TestMerge.f1 property
// // const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
// // "instanceOfTestMerge"
// // );
// // const propertyBeingAssigned = getPropertyBeingAssigned(
// // instanceVariable,
// // "f1"
// // );

// // // Find definition of TestMerge.f1 property
// // const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
// // const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
// // SyntaxKind.VariableDeclaration
// // );

// // // Assert that find definition returned the Zod definition of Test
// // expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
// // expect(parentOfProperty?.getName()).toEqual("Test");
// // });

// // test("works for second object properties inferred from z.object().merge()", () => {
// // // Find usage of TestMerge.f2 property
// // const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
// // "instanceOfTestMerge"
// // );
// // const propertyBeingAssigned = getPropertyBeingAssigned(
// // instanceVariable,
// // "f2"
// // );

// // // Find definition of TestMerge.f2 property
// // const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
// // const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
// // SyntaxKind.VariableDeclaration
// // );

// // // Assert that find definition returned the Zod definition of TestMerge
// // expect(definitionOfProperty?.getText()).toEqual(
// // "f2: z.string().optional()"
// // );
// // expect(parentOfProperty?.getName()).toEqual("TestMerge");
// // });

// test("works for first object properties inferred from z.union()", () => {
// // Find usage of TestUnion.f1 property
// const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
// "instanceOfTestUnion"
// );
// const propertyBeingAssigned = getPropertyBeingAssigned(
// instanceVariable,
// "f1"
// );

// // Find definition of TestUnion.f1 property
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
// SyntaxKind.VariableDeclaration
// );

// // Assert that find definition returned the Zod definition of Test
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
// expect(parentOfProperty?.getName()).toEqual("Test");
// });

// test("works for second object properties inferred from z.union()", () => {
// // Find usage of TestUnion.f2 property
// const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
// "instanceOfTestUnion"
// );
// const propertyBeingAssigned = getPropertyBeingAssigned(
// instanceVariable,
// "f2"
// );

// // Find definition of TestUnion.f2 property
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
// SyntaxKind.VariableDeclaration
// );

// // Assert that find definition returned the Zod definition of TestUnion
// expect(definitionOfProperty?.getText()).toEqual(
// "f2: z.string().optional()"
// );
// expect(parentOfProperty?.getName()).toEqual("TestUnion");
// });

// test("works for object properties inferred from z.object().partial()", () => {
// // Find usage of TestPartial.f1 property
// const instanceVariable = sourceFile.getVariableDeclarationOrThrow(
// "instanceOfTestPartial"
// );
// const propertyBeingAssigned = getPropertyBeingAssigned(
// instanceVariable,
// "f1"
// );

// // Find definition of TestPartial.f1 property
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
// SyntaxKind.VariableDeclaration
// );

// // Assert that find definition returned the Zod definition of Test
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
// expect(parentOfProperty?.getName()).toEqual("Test");
// });

// test("works for object properties inferred from z.object().pick()", () => {
// // Find usage of TestPick.f1 property
// const instanceVariable =
// sourceFile.getVariableDeclarationOrThrow("instanceOfTestPick");
// const propertyBeingAssigned = getPropertyBeingAssigned(
// instanceVariable,
// "f1"
// );

// // Find definition of TestPick.f1 property
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
// SyntaxKind.VariableDeclaration
// );

// // Assert that find definition returned the Zod definition of Test
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
// expect(parentOfProperty?.getName()).toEqual("Test");
// });

// test("works for object properties inferred from z.object().omit()", () => {
// // Find usage of TestOmit.f1 property
// const instanceVariable =
// sourceFile.getVariableDeclarationOrThrow("instanceOfTestOmit");
// const propertyBeingAssigned = getPropertyBeingAssigned(
// instanceVariable,
// "f1"
// );

// // Find definition of TestOmit.f1 property
// const definitionOfProperty = propertyBeingAssigned?.getDefinitionNodes()[0];
// const parentOfProperty = definitionOfProperty?.getFirstAncestorByKind(
// SyntaxKind.VariableDeclaration
// );

// // Assert that find definition returned the Zod definition of Test
// expect(definitionOfProperty?.getText()).toEqual("f1: z.number()");
// expect(parentOfProperty?.getName()).toEqual("Test");
// });
// });

// const getPropertyBeingAssigned = (node: Node, name: string) => {
// const propertyAssignment = node.forEachDescendant((descendent) =>
// Node.isPropertyAssignment(descendent) && descendent.getName() == name
// ? descendent
// : undefined
// );

// if (propertyAssignment == null)
// fail(`Could not find property assignment with name ${name}`);

// const propertyLiteral = propertyAssignment.getFirstDescendantByKind(
// SyntaxKind.Identifier
// );

// if (propertyLiteral == null)
// fail(`Could not find property literal with name ${name}`);

// return propertyLiteral;
// };
Loading

0 comments on commit 898dced

Please sign in to comment.