|
1 | 1 | import { test } from "@oclif/test";
|
| 2 | +import fs from "fs"; |
| 3 | +import { sep, posix } from "path"; |
2 | 4 |
|
3 | 5 | /**
|
4 | 6 | * For the CLI tests to run, we need to run them in a Node environment with
|
5 | 7 | * the NODE_OPTIONS=--experimental-vm-modules flag. This is because Jest ships
|
6 | 8 | * with experimental support for ECMAScript Modules (ESM).
|
7 | 9 | * See: https://jestjs.io/docs/ecmascript-modules
|
8 | 10 | */
|
9 |
| -describe("CLI Tests", () => { |
| 11 | +describe("Oclif-provided Flags Tests", () => { |
10 | 12 | describe("--help flag", () => {
|
11 | 13 | test
|
12 | 14 | .stdout()
|
@@ -55,3 +57,59 @@ describe("CLI Tests", () => {
|
55 | 57 | });
|
56 | 58 | });
|
57 | 59 | });
|
| 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 | +} |
0 commit comments