forked from openapi-ts/openapi-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.test.ts
167 lines (155 loc) · 5.12 KB
/
cli.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { execa } from "execa";
import fs from "node:fs";
import os from "node:os";
import { fileURLToPath } from "node:url";
import stripAnsi from "strip-ansi";
import type { TestCase } from "./test-helpers.js";
const root = new URL("../", import.meta.url);
const cwd = os.platform() === "win32" ? fileURLToPath(root) : root; // execa bug: fileURLToPath required on Windows
const cmd = "./bin/cli.js";
const TIMEOUT = 90_000;
describe("CLI", () => {
const tests: TestCase<any, any>[] = [
[
"snapshot > GitHub API",
{
given: ["./examples/github-api.yaml"],
want: new URL("./examples/github-api.ts", root),
ci: { timeout: TIMEOUT },
},
],
[
"snapshot > GitHub API (immutable)",
{
given: ["./examples/github-api.yaml", "--immutable"],
want: new URL("./examples/github-api-immutable.ts", root),
ci: { timeout: TIMEOUT },
},
],
[
"snapshot > GitHub API (types + immutable)",
{
given: ["./examples/github-api.yaml", "--immutable", "--export-type"],
want: new URL("./examples/github-api-export-type-immutable.ts", root),
ci: { timeout: TIMEOUT },
},
],
[
"snapshot > GitHub API (root types)",
{
given: ["./examples/github-api.yaml", "--root-types"],
want: new URL("./examples/github-api-root-types.ts", root),
ci: { timeout: TIMEOUT },
},
],
[
"snapshot > GitHub API (next)",
{
given: ["./examples/github-api-next.yaml"],
want: new URL("./examples/github-api-next.ts", root),
ci: { timeout: TIMEOUT },
},
],
[
"snapshot > Octokit GHES 3.6 Diff to API",
{
given: ["./examples/octokit-ghes-3.6-diff-to-api.json"],
want: new URL("./examples/octokit-ghes-3.6-diff-to-api.ts", root),
ci: { timeout: TIMEOUT },
},
],
[
"snapshot > Stripe API",
{
given: ["./examples/stripe-api.yaml"],
want: new URL("./examples/stripe-api.ts", root),
ci: { timeout: TIMEOUT },
},
],
[
"snapshot > DigitalOcean",
{
given: ["./examples/digital-ocean-api/DigitalOcean-public.v2.yaml"],
want: new URL("./examples/digital-ocean-api.ts", root),
ci: { timeout: TIMEOUT },
},
],
];
for (const [testName, { given, want, ci }] of tests) {
test.skipIf(ci?.skipIf)(
testName,
async () => {
const { stdout } = await execa(cmd, given, { cwd });
if (want instanceof URL) {
expect(stdout).toMatchFileSnapshot(fileURLToPath(want));
} else {
expect(stdout).toBe(`${want}\n`);
}
},
ci?.timeout,
);
}
test(
"stdin",
async () => {
const input = fs.readFileSync(new URL("./examples/stripe-api.yaml", root));
const { stdout } = await execa(cmd, { input, cwd });
expect(stdout).toMatchFileSnapshot(fileURLToPath(new URL("./examples/stripe-api.ts", root)));
},
TIMEOUT,
);
describe("flags", () => {
test("--help", async () => {
const { stdout } = await execa(cmd, ["--help"], { cwd });
expect(stdout).toEqual(expect.stringMatching(/^Usage\n\s+\$ openapi-typescript \[input\] \[options\]/));
});
test("--version", async () => {
const { stdout } = await execa(cmd, ["--version"], { cwd });
expect(stdout).toEqual(expect.stringMatching(/^v[\d.]+(-.*)?$/));
});
test(
"--properties-required-by-default",
async () => {
const { stdout } = await execa(cmd, ["--properties-required-by-default=true", "./examples/github-api.yaml"], {
cwd,
});
expect(stdout).toMatchFileSnapshot(fileURLToPath(new URL("./examples/github-api-required.ts", root)));
},
TIMEOUT,
);
});
describe("Redocly config", () => {
test.skipIf(os.platform() === "win32")("automatic config", async () => {
const cwd = new URL("./fixtures/redocly/", import.meta.url);
await execa("../../../bin/cli.js", {
cwd: fileURLToPath(cwd),
});
for (const schema of ["a", "b", "c"]) {
expect(fs.readFileSync(new URL(`./output/${schema}.ts`, cwd), "utf8")).toMatchFileSnapshot(
fileURLToPath(new URL("../../../examples/simple-example.ts", cwd)),
);
}
});
test("--redocly config", async () => {
await execa(cmd, ["--redocly", "test/fixtures/redocly-flag/redocly.yaml"], {
cwd,
});
for (const schema of ["a", "b", "c"]) {
expect(
fs.readFileSync(new URL(`./test/fixtures/redocly-flag/output/${schema}.ts`, root), "utf8"),
).toMatchFileSnapshot(fileURLToPath(new URL("./examples/simple-example.ts", root)));
}
});
test.skipIf(os.platform() === "win32")("lint error", async () => {
const cwd = new URL("./fixtures/redocly-lint-error", import.meta.url);
try {
await execa("../../../bin/cli.js", {
cwd: fileURLToPath(cwd),
});
throw new Error("Linting should have thrown an error");
} catch (err) {
expect(stripAnsi(String(err))).toMatch(/✘ {2}Servers must be present/);
}
});
});
});