Skip to content

Commit

Permalink
🧪 Jest couldn't take it
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Terdell committed Nov 15, 2023
1 parent b8f2ac7 commit 9991411
Show file tree
Hide file tree
Showing 32 changed files with 2,405 additions and 8,083 deletions.
7,820 changes: 915 additions & 6,905 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "zod-to-json-schema",
"version": "3.21.4",
"description": "Converts Zod schemas to Json Schemas",
"main": "index.js",
"scripts": {
"test": "jest",
"dev": "jest --watch",
"gen": "tsx test/createIndex.ts",
"test": "tsx test/index.ts",
"dev": "tsx watch test/index.ts",
"build": "npm i && jest && rimraf dist && tsc && node copyPackageFiles.js",
"dryrun": "npm run build && npm pub ./dist/ --dry-run"
},
Expand Down Expand Up @@ -40,16 +40,16 @@
"zod": "^3.21.4"
},
"devDependencies": {
"@types/jest": "^26.0.24",
"@types/json-schema": "^7.0.9",
"@types/node": "^20.9.0",
"ajv": "^8.6.3",
"ajv-formats": "^2.1.1",
"jest": "^29.5.0",
"fast-diff": "^1.3.0",
"json-schema-deref-sync": "^0.14.0",
"rimraf": "^3.0.2",
"ts-jest": "^29.1.0",
"tsx": "^4.1.2",
"typescript": "^5.1.3",
"zod": "^3.21.4"
},
"types": "index.d.ts"
}
}
13 changes: 7 additions & 6 deletions test/allParsers.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { zodToJsonSchema } from "../src/zodToJsonSchema";
import { allParsersSchema } from "./allParsersSchema";
import { suite } from "./suite";

describe("All Parsers tests", () => {
it("With JSON schema target, should produce valid json schema (7)", () => {
suite("All Parsers tests", (test) => {
test("With JSON schema target, should produce valid json schema (7)", (assert) => {
const jsonSchema = zodToJsonSchema(allParsersSchema, {
target: "jsonSchema7",
});
Expand Down Expand Up @@ -325,10 +326,10 @@ describe("All Parsers tests", () => {
},
description: "watup",
};
expect(jsonSchema).toStrictEqual(expectedOutput);
assert(jsonSchema, expectedOutput);
});

it("With OpenAPI schema target, should produce valid Open API schema", () => {
test("With OpenAPI schema target, should produce valid Open API schema", (assert) => {
const jsonSchema = zodToJsonSchema(allParsersSchema, {
target: "openApi3",
});
Expand Down Expand Up @@ -678,7 +679,7 @@ describe("All Parsers tests", () => {
string: "hello",
},
description: "watup",
};
expect(jsonSchema).toStrictEqual(expectedOutput);
};
assert(jsonSchema, expectedOutput);
});
});
27 changes: 27 additions & 0 deletions test/createIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { readdirSync, writeFileSync, statSync } from "fs"

function checkDir(dir: string): string[] {
return readdirSync(dir).reduce((a: string[], n) => {
const f = `${dir}/${n}`

const s = statSync(f)

if (s.isFile() && n.endsWith(".test.ts")) {
a.push(f)
}

if (s.isDirectory()) {
a.push(...checkDir(f))
}

return a
}, [])
}

writeFileSync(
"./test/index.ts",
checkDir("./test")
.map((f) => `import "./${f.slice(7, -3)}.js"`)
.join("\n"),
"utf-8",
)
27 changes: 27 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import "./allParsers.test.js"
import "./meta.test.js"
import "./openApiMode.test.js"
import "./parseDef.test.js"
import "./parsers/array.test.js"
import "./parsers/bigint.test.js"
import "./parsers/branded.test.js"
import "./parsers/date.test.js"
import "./parsers/default.test.js"
import "./parsers/effects.test.js"
import "./parsers/intersection.test.js"
import "./parsers/map.test.js"
import "./parsers/nativeEnum.test.js"
import "./parsers/nullable.test.js"
import "./parsers/number.test.js"
import "./parsers/object.test.js"
import "./parsers/optional.test.js"
import "./parsers/pipe.test.js"
import "./parsers/promise.test.js"
import "./parsers/record.test.js"
import "./parsers/set.test.js"
import "./parsers/string.test.js"
import "./parsers/tuple.test.js"
import "./parsers/union.test.js"
import "./readme.test.js"
import "./references.test.js"
import "./zodToJsonSchema.test.js"
16 changes: 8 additions & 8 deletions test/meta.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { JSONSchema7 } from "json-schema";
import { z } from "zod";
import { zodToJsonSchema } from "../src/zodToJsonSchema";

describe("Meta data", () => {
it("should be possible to use description", () => {
import { suite } from "./suite";
suite("Meta data", (it) => {
it("should be possible to use description", (assert) => {
const $z = z.string().describe("My neat string");
const $j = zodToJsonSchema($z);
const $e: JSONSchema7 = {
Expand All @@ -12,10 +12,10 @@ describe("Meta data", () => {
description: "My neat string",
};

expect($j).toStrictEqual($e);
assert($j, $e)
});

it("should be possible to add a markdownDescription", () => {
it("should be possible to add a markdownDescription", (assert) => {
const $z = z.string().describe("My neat string");
const $j = zodToJsonSchema($z, { markdownDescription: true });
const $e = {
Expand All @@ -25,10 +25,10 @@ describe("Meta data", () => {
markdownDescription: "My neat string",
};

expect($j).toStrictEqual($e);
assert($j, $e)
});

it("should handle optional schemas with different descriptions", () => {
it("should handle optional schemas with different descriptions", (assert) => {
const recurringSchema = z.object({});
const zodSchema = z
.object({
Expand All @@ -43,7 +43,7 @@ describe("Meta data", () => {
$refStrategy: "none",
});

expect(jsonSchema).toStrictEqual({
assert(jsonSchema, {
additionalProperties: false,
description: "sssssssss",
properties: {
Expand Down
47 changes: 24 additions & 23 deletions test/openApiMode.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { z } from "zod";
import { zodToJsonSchema } from "../src/zodToJsonSchema";
import { z } from "zod"
import { zodToJsonSchema } from "../src/zodToJsonSchema"
import { suite } from "./suite"

describe("Open API target", () => {
it("should use nullable boolean property and not use $schema property", () => {
suite("Open API target", (test) => {
test("should use nullable boolean property and not use $schema property", (assert) => {
const editCompanySchema = z.object({
companyId: z.string().nullable(),
name: z.string().nullable().optional(),
something: z.literal("hej"),
});
})

const swaggerSchema = zodToJsonSchema(editCompanySchema, {
target: "openApi3",
});
})

const expectedSchema = {
// $schema: "http://json-schema.org/draft-07/schema#",
Expand All @@ -23,17 +24,17 @@ describe("Open API target", () => {
},
required: ["companyId", "something"],
type: "object",
};
}

expect(swaggerSchema).toStrictEqual(expectedSchema);
});
assert(swaggerSchema, expectedSchema)
})

it("should not use the enumNames keyword from the records parser when an enum is present", () => {
const recordSchema = z.record(z.enum(["a", "b", "c"]), z.boolean());
test("should not use the enumNames keyword from the records parser when an enum is present", (assert) => {
const recordSchema = z.record(z.enum(["a", "b", "c"]), z.boolean())

const swaggerSchema = zodToJsonSchema(recordSchema, {
target: "openApi3",
});
})

const expectedSchema = {
type: "object",
Expand All @@ -44,17 +45,17 @@ describe("Open API target", () => {
c: { $ref: "#/properties/a" },
},
additionalProperties: false,
};
}

expect(swaggerSchema).toStrictEqual(expectedSchema);
});
assert(swaggerSchema, expectedSchema)
})

it("should properly reference nullable schemas", () => {
test("should properly reference nullable schemas", (assert) => {
const legalReasonSchema = z
.object({
reason: z.enum(["FOO", "BAR"]),
})
.strict();
.strict()

const identityRequestSchema = z
.object({
Expand All @@ -67,11 +68,11 @@ describe("Open API target", () => {
.array(legalReasonSchema.shape.reason)
.nullish(), // reused here
})
.strict();
.strict()

const result = zodToJsonSchema(identityRequestSchema, {
target: "openApi3",
});
})

const expected = {
type: "object",
Expand Down Expand Up @@ -99,8 +100,8 @@ describe("Open API target", () => {
},
required: ["alias"],
additionalProperties: false,
};
}

expect(result).toStrictEqual(expected);
});
});
assert(result, expected)
})
})
47 changes: 25 additions & 22 deletions test/parseDef.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { JSONSchema7Type } from "json-schema";
import { z } from "zod";
import { parseDef } from "../src/parseDef";
import Ajv from "ajv";
import { getRefs } from "../src/Refs";
const ajv = new Ajv();
describe("Basic parsing", () => {
it("should return a proper json schema with some common types without validation", () => {
import { JSONSchema7Type } from "json-schema"
import { z } from "zod"
import { parseDef } from "../src/parseDef"
import Ajv from "ajv"
import { getRefs } from "../src/Refs"
const ajv = new Ajv()

import { suite } from "./suite"

suite("Basic parsing", (test) => {
test("should return a proper json schema with some common types without validation", (assert) => {
const zodSchema = z.object({
requiredString: z.string(),
optionalString: z.string().optional(),
Expand All @@ -25,7 +28,7 @@ describe("Basic parsing", () => {
]),
objectOrNull: z.object({ myString: z.string() }).nullable(),
passthrough: z.object({ myString: z.string() }).passthrough(),
});
})
const expectedJsonSchema: JSONSchema7Type = {
type: "object",
properties: {
Expand Down Expand Up @@ -150,19 +153,19 @@ describe("Basic parsing", () => {
"passthrough",
],
additionalProperties: false,
};
const parsedSchema = parseDef(zodSchema._def, getRefs());
expect(parsedSchema).toStrictEqual(expectedJsonSchema);
expect(ajv.validateSchema(parsedSchema!)).toEqual(true);
});
}
const parsedSchema = parseDef(zodSchema._def, getRefs())
assert(parsedSchema, expectedJsonSchema)
assert(ajv.validateSchema(parsedSchema!), true)
})

it("should handle a nullable string properly", () => {
const shorthand = z.string().nullable();
const union = z.union([z.string(), z.null()]);
test("should handle a nullable string properly", (assert) => {
const shorthand = z.string().nullable()
const union = z.union([z.string(), z.null()])

const expected = { type: ["string", "null"] };
const expected = { type: ["string", "null"] }

expect(parseDef(shorthand._def, getRefs())).toStrictEqual(expected);
expect(parseDef(union._def, getRefs())).toStrictEqual(expected);
});
});
assert(parseDef(shorthand._def, getRefs()), expected)
assert(parseDef(union._def, getRefs()), expected)
})
})
Loading

0 comments on commit 9991411

Please sign in to comment.