Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] test(cli): setting-up some tests for the CLI #233

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@ dist
build
lib
.vscode
src/cli/fixtures/**/*.zod.ts
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -22,8 +22,8 @@
"prepublishOnly": "yarn test:ci && rimraf lib && yarn build",
"lint": "eslint . --ext=.js,.ts,.json --report-unused-disable-directives --max-warnings 0",
"lint:fix": "npm run lint -- --fix",
"test": "jest",
"test:ci": "jest --ci --coverage && yarn gen:all && tsc --noEmit",
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest",
"test:ci": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --ci --coverage && yarn gen:all && tsc --noEmit",
"gen:all": "./bin/run --all",
"gen:example": "./bin/run --config example",
"gen:config": "./bin/run --config config",
Expand Down Expand Up @@ -59,6 +59,7 @@
"@babel/core": "^7.23.2",
"@babel/preset-env": "^7.23.2",
"@babel/preset-typescript": "^7.23.2",
"@oclif/test": "^3.2.11",
"@types/fs-extra": "^11.0.3",
"@types/inquirer": "^8.1.3",
"@types/jest": "^29.5.7",
Expand All @@ -69,6 +70,7 @@
"auto-changelog": "^2.4.0",
"babel-jest": "^29.7.0",
"codecov": "^3.8.3",
"cross-env": "^7.0.3",
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.1",
Expand Down
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!
"
`;
115 changes: 115 additions & 0 deletions src/cli.test.ts
@@ -0,0 +1,115 @@
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("Oclif-provided Flags Tests", () => {
describe("--help flag", () => {
test
.stdout()
.command([".", "--help"])

// --help flag works with an early exit so we need to catch it first
// See: https://github.com/oclif/test/issues/40#issuecomment-1299565083
.catch(/EEXIT: 0/)
.it("should provide the right help message", (ctx) => {
expect(ctx.stdout).toMatchInlineSnapshot(`
"Generate Zod schemas from a Typescript file

USAGE
$ ts-to-zod --all
$ ts-to-zod --config example
$ ts-to-zod --config example/person
$ ts-to-zod --config config

ARGUMENTS
INPUT input file (typescript)
OUTPUT output file (zod schemas)

FLAGS
-a, --all Execute all configs
-c, --config=<option> Execute one config
<options: example|example/person|config>
-h, --help Show CLI help.
-i, --init Create a ts-to-zod.config.js file
-k, --keepComments Keep parameters comments
-v, --version Show CLI version.
-w, --watch Watch input file(s) for changes and re-run
related task
--inferredTypes=<value> Path of z.infer<> types file
--skipParseJSDoc Skip the creation of zod validators from JSDoc
annotations
--skipValidation Skip the validation step (not recommended)

DESCRIPTION
Generate Zod schemas from a Typescript file

EXAMPLES
$ ts-to-zod src/types.ts src/types.zod.ts

"
`);
});
});
});

// 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(),
});