Skip to content

Commit

Permalink
test: navigating the config prompt to ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
tvillaren committed May 1, 2024
1 parent 0bb0d00 commit dad67e2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@ dist
build
lib
.vscode
src/cli/fixtures/**/*.zod.ts
20 changes: 20 additions & 0 deletions src/__snapshots__/cli.test.ts.snap
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Config Prompt Tests Skip config prompt should have selected the right option and generated the file not in the config 1`] = `
"? You have multiple configs available in "ts-to-zod.config.js"
What do you want? (Use arrow keys)
❯ Execute all configs (--all)
Execute "example" config (--config=example)
Execute "example/person" config (--config=example/person)
Execute "config" config (--config=config)
Don't use the config ? You have multiple configs available in "ts-to-zod.config.js"
What do you want?
Execute all configs (--all)
Execute "example" config (--config=example)
Execute "example/person" config (--config=example/person)
Execute "config" config (--config=config)
❯ Don't use the config ? You have multiple configs available in "ts-to-zod.config.js"
What do you want? Don't use the config
🎉 Zod schemas generated!
"
`;
60 changes: 59 additions & 1 deletion src/cli.test.ts
@@ -1,12 +1,14 @@
import { test } from "@oclif/test";
import fs from "fs";
import { sep, posix } from "path";

/**
* For the CLI tests to run, we need to run them in a Node environment with
* the NODE_OPTIONS=--experimental-vm-modules flag. This is because Jest ships
* with experimental support for ECMAScript Modules (ESM).
* See: https://jestjs.io/docs/ecmascript-modules
*/
describe("CLI Tests", () => {
describe("Oclif-provided Flags Tests", () => {
describe("--help flag", () => {
test
.stdout()
Expand Down Expand Up @@ -55,3 +57,59 @@ describe("CLI Tests", () => {
});
});
});

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

describe("Config Prompt Tests", () => {
describe("Skip config prompt", () => {
const basicInputPath = makePosixPath("src/cli/fixtures/basic/input.ts");
const basicSnapshotPath = makePosixPath(
"src/cli/fixtures/basic/output.zod.snapshot.ts"
);
const basicOutputPath = makePosixPath(
"src/cli/fixtures/basic/output.zod.ts"
);

test
// Up Arrow key code \u001B[A + ENTER key code \n with a delay of 2000ms
.stdin("\u001B[A\n", 2000)
.stdout()
.stderr()
.command([".", basicInputPath, basicOutputPath])
.it(
"should have selected the right option and generated the file not in the config",
(ctx) => {
expect(normalizeLineEndings(ctx.stdout)).toMatchSnapshot();

// Ora spinner outputs to stderr by default, we
expect(ctx.stderr).toContain("- Validating generated types");
expect(ctx.stderr).toContain("✔ Validating generated types");

expect(
normalizeLineEndings(
fs.readFileSync(basicOutputPath, "utf-8").toString()
)
).toEqual(
normalizeLineEndings(
fs.readFileSync(basicSnapshotPath, "utf-8").toString()
)
);

removeFile(basicOutputPath);
}
);
});
});

function removeFile(filePath: string) {
fs.unlinkSync(filePath);
}

function makePosixPath(str: string) {
return str.split(sep).join(posix.sep);
}

function normalizeLineEndings(content: string) {
return content.replace(/\r\n/g, "\n"); // Replace Windows (\r\n) with Unix (\n)
}
4 changes: 4 additions & 0 deletions src/cli/fixtures/basic/input.ts
@@ -0,0 +1,4 @@
export type Test = {
name: string;
age: number;
};
7 changes: 7 additions & 0 deletions src/cli/fixtures/basic/output.zod.snapshot.ts
@@ -0,0 +1,7 @@
// Generated by ts-to-zod
import { z } from "zod";

export const testSchema = z.object({
name: z.string(),
age: z.number(),
});

0 comments on commit dad67e2

Please sign in to comment.