Skip to content

Commit eebe6a6

Browse files
committed
Use Vitest .toMatchFileSnapshot()
1 parent e39324e commit eebe6a6

File tree

4 files changed

+15
-37
lines changed

4 files changed

+15
-37
lines changed

packages/openapi-typescript/test/cjs.test.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
// important: MUST use require()!
44
const fs = require("node:fs");
5-
const { URL } = require("node:url");
5+
const { fileURLToPath, URL } = require("node:url");
66
const openapiTS = require("../dist/index.cjs");
77
const yaml = require("js-yaml");
88

9-
// note: this import is fine; it’s just a test helper
10-
import { readFile } from "./helpers.js";
11-
129
describe("CJS bundle", () => {
1310
it("basic", async () => {
1411
const input = yaml.load(fs.readFileSync(new URL("../examples/stripe-api.yaml", import.meta.url), "utf8"));
1512
const output = await openapiTS(input);
16-
expect(output).toBe(readFile(new URL("../examples/stripe-api.ts", import.meta.url)));
13+
expect(output).toMatchFileSnapshot(fileURLToPath(new URL("../examples/stripe-api.ts", import.meta.url)));
1714
});
1815
});

packages/openapi-typescript/test/cli.test.ts

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { execa } from "execa";
2+
import fs from "node:fs";
23
import { URL, fileURLToPath } from "node:url";
34
import os from "node:os";
4-
import { readFile } from "./helpers.js";
55

66
const root = new URL("../", import.meta.url);
77
const cwd = os.platform() === "win32" ? fileURLToPath(root) : root; // execa bug: fileURLToPath required on Windows
@@ -14,58 +14,52 @@ describe("CLI", () => {
1414
test(
1515
"GitHub API",
1616
async () => {
17-
const expected = readFile(new URL("./examples/github-api.ts", root)).trim();
1817
const { stdout } = await execa(cmd, ["./examples/github-api.yaml"], { cwd });
19-
expect(stdout).toBe(expected);
18+
expect(stdout).toMatchFileSnapshot(fileURLToPath(new URL("./examples/github-api.ts", root)));
2019
},
2120
TIMEOUT,
2221
);
2322
test(
2423
"GitHub API (next)",
2524
async () => {
26-
const expected = readFile(new URL("./examples/github-api-next.ts", root)).trim();
2725
const { stdout } = await execa(cmd, ["./examples/github-api-next.yaml"], { cwd });
28-
expect(stdout).toBe(expected);
26+
expect(stdout).toMatchFileSnapshot(fileURLToPath(new URL("./examples/github-api-next.ts", root)));
2927
},
3028
TIMEOUT,
3129
);
3230
test(
3331
"Octokit GHES 3.6 Diff to API",
3432
async () => {
35-
const expected = readFile(new URL("./examples/octokit-ghes-3.6-diff-to-api.ts", root)).trim();
3633
const { stdout } = await execa(cmd, ["./examples/octokit-ghes-3.6-diff-to-api.json"], { cwd });
37-
expect(stdout).toBe(expected);
34+
expect(stdout).toMatchFileSnapshot(fileURLToPath(new URL("./examples/octokit-ghes-3.6-diff-to-api.ts", root)));
3835
},
3936
TIMEOUT,
4037
);
4138
test(
4239
"Stripe API",
4340
async () => {
44-
const expected = readFile(new URL("./examples/stripe-api.ts", root)).trim();
4541
const { stdout } = await execa(cmd, ["./examples/stripe-api.yaml"], { cwd });
46-
expect(stdout).toBe(expected);
42+
expect(stdout).toMatchFileSnapshot(fileURLToPath(new URL("./examples/stripe-api.ts", root)));
4743
},
4844
TIMEOUT,
4945
);
5046
// this test runs too slowly on macos / windows in GitHub Actions (but not natively)
5147
test.skipIf(process.env.CI_ENV === "macos" || process.env.CI_ENV === "windows")(
5248
"DigitalOcean API (remote $refs)",
5349
async () => {
54-
const expected = readFile(new URL("./examples/digital-ocean-api.ts", root)).trim();
5550
const { stdout } = await execa(cmd, ["./examples/digital-ocean-api/DigitalOcean-public.v2.yaml"], {
5651
cwd,
5752
});
58-
expect(stdout).toBe(expected);
53+
expect(stdout).toMatchFileSnapshot(fileURLToPath(new URL("./examples/digital-ocean-api.ts", root)));
5954
},
6055
TIMEOUT,
6156
);
6257
test(
6358
"stdin",
6459
async () => {
65-
const expected = readFile(new URL("./examples/stripe-api.ts", root)).trim();
66-
const input = readFile(new URL("./examples/stripe-api.yaml", root));
60+
const input = fs.readFileSync(new URL("./examples/stripe-api.yaml", root));
6761
const { stdout } = await execa(cmd, { input });
68-
expect(stdout).toBe(expected);
62+
expect(stdout).toMatchFileSnapshot(fileURLToPath(new URL("./examples/stripe-api.ts", root)));
6963
},
7064
TIMEOUT,
7165
);

packages/openapi-typescript/test/helpers.ts

-12
This file was deleted.

packages/openapi-typescript/test/index.test.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { URL, fileURLToPath } from "node:url";
33
import yaml from "js-yaml";
44
import openapiTS from "../dist/index.js";
55
import type { OpenAPI3 } from "../src/types.js";
6-
import { readFile } from "./helpers.js";
76

87
const BOILERPLATE = `/**
98
* This file was auto-generated by openapi-typescript.
@@ -1355,25 +1354,25 @@ export type operations = Record<string, never>;
13551354
describe("GitHub", () => {
13561355
test("default options", async () => {
13571356
const generated = await openapiTS(new URL("./github-api.yaml", EXAMPLES_DIR));
1358-
expect(generated).toBe(readFile(new URL("./github-api.ts", EXAMPLES_DIR)));
1357+
expect(generated).toMatchFileSnapshot(fileURLToPath(new URL("./github-api.ts", EXAMPLES_DIR)));
13591358
}, 30000);
13601359
});
13611360
describe("GitHub (next)", () => {
13621361
test("default options", async () => {
13631362
const generated = await openapiTS(new URL("./github-api-next.yaml", EXAMPLES_DIR));
1364-
expect(generated).toBe(readFile(new URL("./github-api-next.ts", EXAMPLES_DIR)));
1363+
expect(generated).toMatchFileSnapshot(fileURLToPath(new URL("./github-api-next.ts", EXAMPLES_DIR)));
13651364
}, 30000);
13661365
});
13671366
describe("Octokit GHES 3.6 Diff to API", () => {
13681367
test("default options", async () => {
13691368
const generated = await openapiTS(new URL("./octokit-ghes-3.6-diff-to-api.json", EXAMPLES_DIR));
1370-
expect(generated).toBe(readFile(new URL("./octokit-ghes-3.6-diff-to-api.ts", EXAMPLES_DIR)));
1369+
expect(generated).toMatchFileSnapshot(fileURLToPath(new URL("./octokit-ghes-3.6-diff-to-api.ts", EXAMPLES_DIR)));
13711370
}, 30000);
13721371
});
13731372
describe("Stripe", () => {
13741373
test("default options", async () => {
13751374
const generated = await openapiTS(new URL("./stripe-api.yaml", EXAMPLES_DIR));
1376-
expect(generated).toBe(readFile(new URL("./stripe-api.ts", EXAMPLES_DIR)));
1375+
expect(generated).toMatchFileSnapshot(fileURLToPath(new URL("./stripe-api.ts", EXAMPLES_DIR)));
13771376
}, 30000);
13781377
});
13791378
describe("DigitalOcean", () => {
@@ -1382,7 +1381,7 @@ export type operations = Record<string, never>;
13821381
"default options",
13831382
async () => {
13841383
const generated = await openapiTS(new URL("./digital-ocean-api/DigitalOcean-public.v2.yaml", EXAMPLES_DIR));
1385-
expect(generated).toBe(readFile(new URL("./digital-ocean-api.ts", EXAMPLES_DIR)));
1384+
expect(generated).toMatchFileSnapshot(fileURLToPath(new URL("./digital-ocean-api.ts", EXAMPLES_DIR)));
13861385
},
13871386
60000,
13881387
);

0 commit comments

Comments
 (0)