Skip to content

Commit 3f3780c

Browse files
committed
chore(deps): update to oclif/test 4
1 parent dad67e2 commit 3f3780c

File tree

3 files changed

+79
-283
lines changed

3 files changed

+79
-283
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"@babel/core": "^7.23.2",
6060
"@babel/preset-env": "^7.23.2",
6161
"@babel/preset-typescript": "^7.23.2",
62-
"@oclif/test": "^3.2.11",
62+
"@oclif/test": "^4.0.3",
6363
"@types/fs-extra": "^11.0.3",
6464
"@types/inquirer": "^8.1.3",
6565
"@types/jest": "^29.5.7",
@@ -76,6 +76,7 @@
7676
"eslint-plugin-prettier": "^5.0.1",
7777
"husky": "^8.0.3",
7878
"jest": "^29.7.0",
79+
"mock-stdin": "^1.0.0",
7980
"pretty-quick": "^3.1.0",
8081
"rimraf": "^5.0.5",
8182
"ts-node": "^10.9.1"

src/cli.test.ts

+54-45
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test } from "@oclif/test";
1+
import { runCommand } from "@oclif/test";
22
import fs from "fs";
33
import { sep, posix } from "path";
44

@@ -10,15 +10,10 @@ import { sep, posix } from "path";
1010
*/
1111
describe("Oclif-provided Flags Tests", () => {
1212
describe("--help flag", () => {
13-
test
14-
.stdout()
15-
.command([".", "--help"])
16-
17-
// --help flag works with an early exit so we need to catch it first
18-
// See: https://github.com/oclif/test/issues/40#issuecomment-1299565083
19-
.catch(/EEXIT: 0/)
20-
.it("should provide the right help message", (ctx) => {
21-
expect(ctx.stdout).toMatchInlineSnapshot(`
13+
it("should provide the right help message", async () => {
14+
const { stdout } = await runCommand([".", "--help"]);
15+
16+
expect(stdout).toMatchInlineSnapshot(`
2217
"Generate Zod schemas from a Typescript file
2318
2419
USAGE
@@ -54,51 +49,61 @@ describe("Oclif-provided Flags Tests", () => {
5449
5550
"
5651
`);
57-
});
52+
});
5853
});
5954
});
6055

6156
// describe("Ts-to-zod flags Tests", () => {});
6257
// describe("EXIT codes Tests", () => {});
6358

6459
describe("Config Prompt Tests", () => {
60+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
61+
let stdin: any;
62+
beforeEach(() => {
63+
stdin = require("mock-stdin").stdin();
64+
});
6565
describe("Skip config prompt", () => {
66-
const basicInputPath = makePosixPath("src/cli/fixtures/basic/input.ts");
67-
const basicSnapshotPath = makePosixPath(
68-
"src/cli/fixtures/basic/output.zod.snapshot.ts"
69-
);
70-
const basicOutputPath = makePosixPath(
71-
"src/cli/fixtures/basic/output.zod.ts"
72-
);
73-
74-
test
66+
it("should have selected the right option and generated the file not in the config", async () => {
67+
const basicInputPath = makePosixPath("src/cli/fixtures/basic/input.ts");
68+
const basicSnapshotPath = makePosixPath(
69+
"src/cli/fixtures/basic/output.zod.snapshot.ts"
70+
);
71+
const basicOutputPath = makePosixPath(
72+
"src/cli/fixtures/basic/output.zod.ts"
73+
);
74+
7575
// Up Arrow key code \u001B[A + ENTER key code \n with a delay of 2000ms
76-
.stdin("\u001B[A\n", 2000)
77-
.stdout()
78-
.stderr()
79-
.command([".", basicInputPath, basicOutputPath])
80-
.it(
81-
"should have selected the right option and generated the file not in the config",
82-
(ctx) => {
83-
expect(normalizeLineEndings(ctx.stdout)).toMatchSnapshot();
84-
85-
// Ora spinner outputs to stderr by default, we
86-
expect(ctx.stderr).toContain("- Validating generated types");
87-
expect(ctx.stderr).toContain("✔ Validating generated types");
88-
89-
expect(
90-
normalizeLineEndings(
91-
fs.readFileSync(basicOutputPath, "utf-8").toString()
92-
)
93-
).toEqual(
94-
normalizeLineEndings(
95-
fs.readFileSync(basicSnapshotPath, "utf-8").toString()
96-
)
97-
);
98-
99-
removeFile(basicOutputPath);
100-
}
76+
setTimeout(() => stdin.send("\u001B[A\n"), 2000);
77+
78+
const { stdout, stderr } = await runCommand([
79+
".",
80+
basicInputPath,
81+
basicOutputPath,
82+
]);
83+
84+
expect(
85+
replaceAngleBracket(normalizeLineEndings(stdout))
86+
).toMatchSnapshot();
87+
88+
// Ora spinner outputs to stderr by default, we
89+
expect(stderr).toContain("- Validating generated types");
90+
expect(stderr).toContain("✔ Validating generated types");
91+
92+
expect(
93+
normalizeLineEndings(
94+
fs.readFileSync(basicOutputPath, "utf-8").toString()
95+
)
96+
).toEqual(
97+
normalizeLineEndings(
98+
fs.readFileSync(basicSnapshotPath, "utf-8").toString()
99+
)
101100
);
101+
102+
removeFile(basicOutputPath);
103+
});
104+
});
105+
afterEach(() => {
106+
stdin.restore();
102107
});
103108
});
104109

@@ -113,3 +118,7 @@ function makePosixPath(str: string) {
113118
function normalizeLineEndings(content: string) {
114119
return content.replace(/\r\n/g, "\n"); // Replace Windows (\r\n) with Unix (\n)
115120
}
121+
122+
function replaceAngleBracket(content: string) {
123+
return content.replace(/>/g, "❯");
124+
}

0 commit comments

Comments
 (0)