1
- import { test } from "@oclif/test" ;
1
+ import { runCommand } from "@oclif/test" ;
2
2
import fs from "fs" ;
3
3
import { sep , posix } from "path" ;
4
4
@@ -10,15 +10,10 @@ import { sep, posix } from "path";
10
10
*/
11
11
describe ( "Oclif-provided Flags Tests" , ( ) => {
12
12
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 ( / E E X I T : 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 ( `
22
17
"Generate Zod schemas from a Typescript file
23
18
24
19
USAGE
@@ -54,51 +49,61 @@ describe("Oclif-provided Flags Tests", () => {
54
49
55
50
"
56
51
` ) ;
57
- } ) ;
52
+ } ) ;
58
53
} ) ;
59
54
} ) ;
60
55
61
56
// describe("Ts-to-zod flags Tests", () => {});
62
57
// describe("EXIT codes Tests", () => {});
63
58
64
59
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
+ } ) ;
65
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
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
+
75
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
- }
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
+ )
101
100
) ;
101
+
102
+ removeFile ( basicOutputPath ) ;
103
+ } ) ;
104
+ } ) ;
105
+ afterEach ( ( ) => {
106
+ stdin . restore ( ) ;
102
107
} ) ;
103
108
} ) ;
104
109
@@ -113,3 +118,7 @@ function makePosixPath(str: string) {
113
118
function normalizeLineEndings ( content : string ) {
114
119
return content . replace ( / \r \n / g, "\n" ) ; // Replace Windows (\r\n) with Unix (\n)
115
120
}
121
+
122
+ function replaceAngleBracket ( content : string ) {
123
+ return content . replace ( / > / g, "❯" ) ;
124
+ }
0 commit comments