Skip to content

Commit dad67e2

Browse files
committed
test: navigating the config prompt to ignore
1 parent 0bb0d00 commit dad67e2

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ dist
77
build
88
lib
99
.vscode
10+
src/cli/fixtures/**/*.zod.ts

src/__snapshots__/cli.test.ts.snap

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Config Prompt Tests Skip config prompt should have selected the right option and generated the file not in the config 1`] = `
4+
"? You have multiple configs available in "ts-to-zod.config.js"
5+
What do you want? (Use arrow keys)
6+
❯ Execute all configs (--all)
7+
Execute "example" config (--config=example)
8+
Execute "example/person" config (--config=example/person)
9+
Execute "config" config (--config=config)
10+
Don't use the config ? You have multiple configs available in "ts-to-zod.config.js"
11+
What do you want?
12+
Execute all configs (--all)
13+
Execute "example" config (--config=example)
14+
Execute "example/person" config (--config=example/person)
15+
Execute "config" config (--config=config)
16+
❯ Don't use the config ? You have multiple configs available in "ts-to-zod.config.js"
17+
What do you want? Don't use the config
18+
🎉 Zod schemas generated!
19+
"
20+
`;

src/cli.test.ts

+59-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { test } from "@oclif/test";
2+
import fs from "fs";
3+
import { sep, posix } from "path";
24

35
/**
46
* For the CLI tests to run, we need to run them in a Node environment with
57
* the NODE_OPTIONS=--experimental-vm-modules flag. This is because Jest ships
68
* with experimental support for ECMAScript Modules (ESM).
79
* See: https://jestjs.io/docs/ecmascript-modules
810
*/
9-
describe("CLI Tests", () => {
11+
describe("Oclif-provided Flags Tests", () => {
1012
describe("--help flag", () => {
1113
test
1214
.stdout()
@@ -55,3 +57,59 @@ describe("CLI Tests", () => {
5557
});
5658
});
5759
});
60+
61+
// describe("Ts-to-zod flags Tests", () => {});
62+
// describe("EXIT codes Tests", () => {});
63+
64+
describe("Config Prompt Tests", () => {
65+
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
75+
// 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+
}
101+
);
102+
});
103+
});
104+
105+
function removeFile(filePath: string) {
106+
fs.unlinkSync(filePath);
107+
}
108+
109+
function makePosixPath(str: string) {
110+
return str.split(sep).join(posix.sep);
111+
}
112+
113+
function normalizeLineEndings(content: string) {
114+
return content.replace(/\r\n/g, "\n"); // Replace Windows (\r\n) with Unix (\n)
115+
}

src/cli/fixtures/basic/input.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type Test = {
2+
name: string;
3+
age: number;
4+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Generated by ts-to-zod
2+
import { z } from "zod";
3+
4+
export const testSchema = z.object({
5+
name: z.string(),
6+
age: z.number(),
7+
});

0 commit comments

Comments
 (0)