Skip to content

Commit b971136

Browse files
committed
chore: prettify test snapshots
1 parent a67778a commit b971136

File tree

3 files changed

+63
-60
lines changed

3 files changed

+63
-60
lines changed

packages/typed-openapi/tests/generate-runtime.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import SwaggerParser from "@apidevtools/swagger-parser";
33
import type { OpenAPIObject } from "openapi3-ts/oas31";
44
import { mapOpenApiEndpoints } from "../src/map-openapi-endpoints.ts";
55
import { allowedRuntimes, generateFile } from "../src/generator.ts";
6+
import { prettify } from "../src/format.ts";
67

78
const samples = ["petstore", "docker.openapi", "long-operation-id"];
89
// @ts-expect-error
@@ -17,10 +18,10 @@ samples.forEach((sample) => {
1718
runtimes.forEach((runtime: string) => {
1819
if (runtime === "arktype" && sample === "docker.openapi") return;
1920

20-
test(`generate ${runtime}`, () => {
21-
const tsRouter = generateFile({ ...ctx, runtime: runtime as any });
21+
test(`generate ${runtime}`, async () => {
22+
const tsRouter = await prettify(generateFile({ ...ctx, runtime: runtime as any }));
2223
const runtimeName = runtime === "none" ? "client" : runtime;
23-
expect(tsRouter).toMatchFileSnapshot(`./snapshots/${sample}.` + runtimeName + ".ts");
24+
await expect(tsRouter).toMatchFileSnapshot(`./snapshots/${sample}.` + runtimeName + ".ts");
2425
});
2526
});
2627
});

packages/typed-openapi/tests/generator-basic-schemas.test.ts

Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { OpenapiSchemaConvertContext, type LibSchemaObject } from "../src/types.
77
import { tsFactory } from "../src/ts-factory.ts";
88
import { mapOpenApiEndpoints } from "../src/map-openapi-endpoints.ts";
99
import { generateFile } from "../src/generator.ts";
10+
import { prettify } from "../src/format.ts";
1011

1112
const factory = tsFactory;
1213
const makeCtx = (schemas: SchemasObject): OpenapiSchemaConvertContext => ({
@@ -15,51 +16,51 @@ const makeCtx = (schemas: SchemasObject): OpenapiSchemaConvertContext => ({
1516
});
1617
const makeDoc = (schemas: SchemasObject) => ({ components: { schemas } } as any);
1718

18-
const getSchemaBox = (schema: LibSchemaObject) => {
19-
const output = generateFile(mapOpenApiEndpoints(makeDoc({ _Test: schema })));
19+
const getSchemaBox = async (schema: LibSchemaObject) => {
20+
const output = await prettify(generateFile(mapOpenApiEndpoints(makeDoc({ _Test: schema }))));
2021
const start = output.indexOf("// <Schemas>");
2122
const end = output.indexOf("// </Schemas>");
2223
return output.substring(start + "// <Schemas>".length, end).trim();
2324
};
2425

2526
test("getSchemaBox", async () => {
26-
expect(getSchemaBox({ type: "null" })).toMatchInlineSnapshot('"export type _Test = null;"');
27-
expect(getSchemaBox({ type: "boolean" })).toMatchInlineSnapshot('"export type _Test = boolean;"');
28-
expect(getSchemaBox({ type: "boolean", nullable: true })).toMatchInlineSnapshot('"export type _Test = boolean | null;"');
29-
expect(getSchemaBox({ type: "string" })).toMatchInlineSnapshot('"export type _Test = string;"');
30-
expect(getSchemaBox({ type: "number" })).toMatchInlineSnapshot('"export type _Test = number;"');
31-
expect(getSchemaBox({ type: "integer" })).toMatchInlineSnapshot('"export type _Test = number;"');
32-
expect(getSchemaBox({})).toMatchInlineSnapshot('"export type _Test = unknown;"');
33-
34-
expect(getSchemaBox({ type: "array", items: { type: "string" } })).toMatchInlineSnapshot('"export type _Test = Array<string>;"');
35-
expect(getSchemaBox({ type: "object" })).toMatchInlineSnapshot(
36-
'"export type _Test = Record<string, unknown>;"',
27+
expect(await getSchemaBox({ type: "null" })).toMatchInlineSnapshot(`"export type _Test = null;"`);
28+
expect(await getSchemaBox({ type: "boolean" })).toMatchInlineSnapshot(`"export type _Test = boolean;"`);
29+
expect(await getSchemaBox({ type: "boolean", nullable: true })).toMatchInlineSnapshot(`"export type _Test = boolean | null;"`);
30+
expect(await getSchemaBox({ type: "string" })).toMatchInlineSnapshot(`"export type _Test = string;"`);
31+
expect(await getSchemaBox({ type: "number" })).toMatchInlineSnapshot(`"export type _Test = number;"`);
32+
expect(await getSchemaBox({ type: "integer" })).toMatchInlineSnapshot(`"export type _Test = number;"`);
33+
expect(await getSchemaBox({})).toMatchInlineSnapshot(`"export type _Test = unknown;"`);
34+
35+
expect(await getSchemaBox({ type: "array", items: { type: "string" } })).toMatchInlineSnapshot(`"export type _Test = Array<string>;"`);
36+
expect(await getSchemaBox({ type: "object" })).toMatchInlineSnapshot(
37+
`"export type _Test = Record<string, unknown>;"`,
3738
);
38-
expect(getSchemaBox({ type: "object", properties: { str: { type: "string" } } })).toMatchInlineSnapshot('"export type _Test = Partial<{ str: string }>;"');
39-
expect(getSchemaBox({ type: "object", properties: { str: { type: "string" }, nb: { type: "number" } } }))
40-
.toMatchInlineSnapshot('"export type _Test = Partial<{ str: string; nb: number }>;"');
39+
expect(await getSchemaBox({ type: "object", properties: { str: { type: "string" } } })).toMatchInlineSnapshot(`"export type _Test = Partial<{ str: string }>;"`);
40+
expect(await getSchemaBox({ type: "object", properties: { str: { type: "string" }, nb: { type: "number" } } }))
41+
.toMatchInlineSnapshot(`"export type _Test = Partial<{ str: string; nb: number }>;"`);
4142

4243
// AllPropertiesRequired
4344
expect(
44-
getSchemaBox({
45+
await getSchemaBox({
4546
type: "object",
4647
properties: { str: { type: "string" }, nb: { type: "number" } },
4748
required: ["str", "nb"],
4849
}),
49-
).toMatchInlineSnapshot('"export type _Test = { str: string; nb: number };"');
50+
).toMatchInlineSnapshot(`"export type _Test = { str: string; nb: number };"`);
5051

5152
// SomeOptionalProps
5253
expect(
53-
getSchemaBox({
54+
await getSchemaBox({
5455
type: "object",
5556
properties: { str: { type: "string" }, nb: { type: "number" } },
5657
required: ["str"],
5758
}),
58-
).toMatchInlineSnapshot('"export type _Test = { str: string; nb?: number | undefined };"');
59+
).toMatchInlineSnapshot(`"export type _Test = { str: string; nb?: number | undefined };"`);
5960

6061
// ObjectWithNestedProp
6162
expect(
62-
getSchemaBox({
63+
await getSchemaBox({
6364
type: "object",
6465
properties: {
6566
str: { type: "string" },
@@ -72,24 +73,24 @@ test("getSchemaBox", async () => {
7273
},
7374
},
7475
}),
75-
).toMatchInlineSnapshot('"export type _Test = Partial<{ str: string; nb: number; nested: Partial<{ nested_prop: boolean }> }>;"');
76+
).toMatchInlineSnapshot(`"export type _Test = Partial<{ str: string; nb: number; nested: Partial<{ nested_prop: boolean }> }>;"`);
7677

7778
// ObjectWithAdditionalPropsNb
7879
expect(
79-
getSchemaBox({ type: "object", properties: { str: { type: "string" } }, additionalProperties: { type: "number" } }),
80-
).toMatchInlineSnapshot('"export type _Test = Partial<{ str: string } & { string: number }>;"');
80+
await getSchemaBox({ type: "object", properties: { str: { type: "string" } }, additionalProperties: { type: "number" } }),
81+
).toMatchInlineSnapshot(`"export type _Test = Partial<{ str: string } & { string: number }>;"`);
8182

8283
// ObjectWithNestedRecordBoolean
8384
expect(
84-
getSchemaBox({
85+
await getSchemaBox({
8586
type: "object",
8687
properties: { str: { type: "string" } },
8788
additionalProperties: { type: "object", properties: { prop: { type: "boolean" } } },
8889
}),
89-
).toMatchInlineSnapshot('"export type _Test = Partial<{ str: string } & { string: Partial<{ prop: boolean }> }>;"');
90+
).toMatchInlineSnapshot(`"export type _Test = Partial<{ str: string } & { string: Partial<{ prop: boolean }> }>;"`);
9091

9192
expect(
92-
getSchemaBox({
93+
await getSchemaBox({
9394
type: "array",
9495
items: {
9596
type: "object",
@@ -98,10 +99,10 @@ test("getSchemaBox", async () => {
9899
},
99100
},
100101
}),
101-
).toMatchInlineSnapshot('"export type _Test = Array<Partial<{ str: string }>>;"');
102+
).toMatchInlineSnapshot(`"export type _Test = Array<Partial<{ str: string }>>;"`);
102103

103104
expect(
104-
getSchemaBox({
105+
await getSchemaBox({
105106
type: "array",
106107
items: {
107108
type: "array",
@@ -110,79 +111,79 @@ test("getSchemaBox", async () => {
110111
},
111112
},
112113
}),
113-
).toMatchInlineSnapshot('"export type _Test = Array<Array<string>>;"');
114+
).toMatchInlineSnapshot(`"export type _Test = Array<Array<string>>;"`);
114115

115116
// ObjectWithEnum
116117
expect(
117-
getSchemaBox({
118+
await getSchemaBox({
118119
type: "object",
119120
properties: {
120121
enumprop: { type: "string", enum: ["aaa", "bbb", "ccc"] },
121122
},
122123
}),
123-
).toMatchInlineSnapshot('"export type _Test = Partial<{ enumprop: "aaa" | "bbb" | "ccc" }>;"');
124+
).toMatchInlineSnapshot(`"export type _Test = Partial<{ enumprop: "aaa" | "bbb" | "ccc" }>;"`);
124125

125-
expect(getSchemaBox({ type: "string", enum: ["aaa", "bbb", "ccc"] })).toMatchInlineSnapshot(
126-
'"export type _Test = "aaa" | "bbb" | "ccc";"',
126+
expect(await getSchemaBox({ type: "string", enum: ["aaa", "bbb", "ccc"] })).toMatchInlineSnapshot(
127+
`"export type _Test = "aaa" | "bbb" | "ccc";"`,
127128
);
128129

129130
// StringENum
130-
expect(getSchemaBox({ type: "string", enum: ["aaa", "bbb", "ccc"] })).toMatchInlineSnapshot('"export type _Test = "aaa" | "bbb" | "ccc";"');
131+
expect(await getSchemaBox({ type: "string", enum: ["aaa", "bbb", "ccc"] })).toMatchInlineSnapshot(`"export type _Test = "aaa" | "bbb" | "ccc";"`);
131132

132133
// ObjectWithUnion
133134
expect(
134-
getSchemaBox({
135+
await getSchemaBox({
135136
type: "object",
136137
properties: {
137138
union: { oneOf: [{ type: "string" }, { type: "number" }] },
138139
},
139140
}),
140-
).toMatchInlineSnapshot('"export type _Test = Partial<{ union: string | number }>;"');
141-
expect(getSchemaBox({ oneOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot(
142-
'"export type _Test = string | number;"',
141+
).toMatchInlineSnapshot(`"export type _Test = Partial<{ union: string | number }>;"`);
142+
expect(await getSchemaBox({ oneOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot(
143+
`"export type _Test = string | number;"`,
143144
);
144145

145146
// StringOrNumber
146-
expect(getSchemaBox({ oneOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot('"export type _Test = string | number;"');
147+
expect(await getSchemaBox({ oneOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot(`"export type _Test = string | number;"`);
147148

148-
expect(getSchemaBox({ allOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot(
149-
'"export type _Test = string & number;"',
149+
expect(await getSchemaBox({ allOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot(
150+
`"export type _Test = string & number;"`,
150151
);
151152

152153
// StringAndNumber
153-
expect(getSchemaBox({ allOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot('"export type _Test = string & number;"');
154+
expect(await getSchemaBox({ allOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot(`"export type _Test = string & number;"`);
154155

155-
expect(getSchemaBox({ anyOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot(
156-
'"export type _Test = string | number | Array<string | number>;"',
156+
expect(await getSchemaBox({ anyOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot(
157+
`"export type _Test = string | number | Array<string | number>;"`,
157158
);
158159

159160
// StringAndNumberMaybeMultiple
160-
expect(getSchemaBox({ anyOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot('"export type _Test = string | number | Array<string | number>;"');
161+
expect(await getSchemaBox({ anyOf: [{ type: "string" }, { type: "number" }] })).toMatchInlineSnapshot(`"export type _Test = string | number | Array<string | number>;"`);
161162

162163
// ObjectWithArrayUnion
163164
expect(
164-
getSchemaBox({
165+
await getSchemaBox({
165166
type: "object",
166167
properties: {
167168
unionOrArrayOfUnion: { anyOf: [{ type: "string" }, { type: "number" }] },
168169
},
169170
}),
170-
).toMatchInlineSnapshot('"export type _Test = Partial<{ unionOrArrayOfUnion: string | number | Array<string | number> }>;"');
171+
).toMatchInlineSnapshot(`"export type _Test = Partial<{ unionOrArrayOfUnion: string | number | Array<string | number> }>;"`);
171172

172173
// ObjectWithIntersection
173174
expect(
174-
getSchemaBox({
175+
await getSchemaBox({
175176
type: "object",
176177
properties: {
177178
intersection: { allOf: [{ type: "string" }, { type: "number" }] },
178179
},
179180
}),
180-
).toMatchInlineSnapshot('"export type _Test = Partial<{ intersection: string & number }>;"');
181+
).toMatchInlineSnapshot(`"export type _Test = Partial<{ intersection: string & number }>;"`);
181182

182-
expect(getSchemaBox({ type: "string", enum: ["aaa", "bbb", "ccc"] })).toMatchInlineSnapshot(
183-
'"export type _Test = "aaa" | "bbb" | "ccc";"',
183+
expect(await getSchemaBox({ type: "string", enum: ["aaa", "bbb", "ccc"] })).toMatchInlineSnapshot(
184+
`"export type _Test = "aaa" | "bbb" | "ccc";"`,
184185
);
185-
expect(getSchemaBox({ type: "number", enum: [1, 2, 3] })).toMatchInlineSnapshot('"export type _Test = 1 | 2 | 3;"');
186+
expect(await getSchemaBox({ type: "number", enum: [1, 2, 3] })).toMatchInlineSnapshot(`"export type _Test = 1 | 2 | 3;"`);
186187
});
187188

188189
describe("getSchemaBox with context", () => {

packages/typed-openapi/tests/generator.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import SwaggerParser from "@apidevtools/swagger-parser";
33
import type { OpenAPIObject } from "openapi3-ts/oas31";
44
import { mapOpenApiEndpoints } from "../src/map-openapi-endpoints.ts";
55
import { generateFile } from "../src/generator.ts";
6+
import { prettify } from "../src/format.ts";
67

78
describe("generator", () => {
89
test("petstore", async ({ expect }) => {
910
const openApiDoc = (await SwaggerParser.parse("./tests/samples/petstore.yaml")) as OpenAPIObject;
10-
expect(generateFile(mapOpenApiEndpoints(openApiDoc))).toMatchInlineSnapshot(`
11+
expect(await prettify(generateFile(mapOpenApiEndpoints(openApiDoc)))).toMatchInlineSnapshot(`
1112
"export namespace Schemas {
1213
// <Schemas>
1314
export type Order = Partial<{
@@ -380,8 +381,8 @@ describe("generator", () => {
380381
`);
381382
});
382383

383-
test("nullable string", ({ expect }) => {
384-
expect(generateFile(mapOpenApiEndpoints({
384+
test("nullable string", async ({ expect }) => {
385+
expect(await prettify(generateFile(mapOpenApiEndpoints({
385386
"openapi": "3.0.0",
386387
"info": {
387388
"version": "1.0.0",
@@ -627,7 +628,7 @@ describe("generator", () => {
627628
}
628629
},
629630
}
630-
}))).toMatchInlineSnapshot(`
631+
})))).toMatchInlineSnapshot(`
631632
"export namespace Schemas {
632633
// <Schemas>
633634
export type SerializedUserSession = {

0 commit comments

Comments
 (0)