@@ -18,18 +18,17 @@ jest.mock("../utils", () => ({
18
18
} ) ) ;
19
19
jest . mock ( "../../list" ) ;
20
20
21
+ // Mock fs and path modules
22
+ jest . mock ( "fs" ) ;
23
+ jest . mock ( "path" ) ;
24
+
21
25
// Create mock functions for fs and path
22
26
const mockExistsSync = jest . fn ( ) ;
23
27
const mockResolve = jest . fn ( ) ;
24
28
25
- // Mock fs and path modules
26
- jest . mock ( "fs" , ( ) => ( {
27
- existsSync : mockExistsSync ,
28
- } ) ) ;
29
-
30
- jest . mock ( "path" , ( ) => ( {
31
- resolve : mockResolve ,
32
- } ) ) ;
29
+ // Override module exports with mocks
30
+ jest . mocked ( require ( "fs" ) ) . existsSync = mockExistsSync ;
31
+ jest . mocked ( require ( "path" ) ) . resolve = mockResolve ;
33
32
34
33
/**
35
34
* Test suite for the list command
@@ -39,15 +38,15 @@ jest.mock("path", () => ({
39
38
describe ( "cmdList" , ( ) => {
40
39
// Store original console.log
41
40
const originalConsoleLog = console . log ;
42
-
41
+
43
42
beforeEach ( ( ) => {
44
43
// Reset all mocks before each test
45
44
jest . clearAllMocks ( ) ;
46
-
45
+
47
46
// Setup default mock implementations
48
47
mockExistsSync . mockReturnValue ( true ) ;
49
48
mockResolve . mockImplementation ( ( ...args ) => args . join ( "/" ) ) ;
50
-
49
+
51
50
// Mock console.log
52
51
console . log = jest . fn ( ) ;
53
52
} ) ;
@@ -68,10 +67,13 @@ describe("cmdList", () => {
68
67
*/
69
68
test ( "displays help message when --help flag is used" , ( ) => {
70
69
const mockPrintAndExit = utils . printAndExit as jest . MockedFunction < typeof utils . printAndExit > ;
71
-
70
+
72
71
cmdList ( [ "--help" ] ) ;
73
-
74
- expect ( mockPrintAndExit ) . toHaveBeenCalledWith ( expect . stringContaining ( "Usage" ) , 0 ) ;
72
+
73
+ expect ( mockPrintAndExit ) . toHaveBeenCalledWith (
74
+ expect . stringContaining ( "Usage" ) ,
75
+ 0
76
+ ) ;
75
77
} ) ;
76
78
77
79
/**
@@ -81,10 +83,12 @@ describe("cmdList", () => {
81
83
test ( "validates out directory exists when specified" , ( ) => {
82
84
const mockPrintAndExit = utils . printAndExit as jest . MockedFunction < typeof utils . printAndExit > ;
83
85
mockExistsSync . mockReturnValue ( false ) ;
84
-
86
+
85
87
cmdList ( [ "--out-dir" , "/non/existent/dir" ] ) ;
86
-
87
- expect ( mockPrintAndExit ) . toHaveBeenCalledWith ( expect . stringContaining ( "/non/existent/dir" ) ) ;
88
+
89
+ expect ( mockPrintAndExit ) . toHaveBeenCalledWith (
90
+ expect . stringContaining ( "/non/existent/dir" )
91
+ ) ;
88
92
} ) ;
89
93
90
94
/**
@@ -94,12 +98,10 @@ describe("cmdList", () => {
94
98
test ( "calls list with correct parameters" , ( ) => {
95
99
const mockList = list as jest . MockedFunction < typeof list > ;
96
100
const args = [ "--out-dir" , "/output/dir" ] ;
97
-
101
+
98
102
cmdList ( args ) ;
99
-
100
- expect ( mockList ) . toHaveBeenCalledWith ( {
101
- outDir : "/output/dir" ,
102
- } ) ;
103
+
104
+ expect ( mockList ) . toHaveBeenCalledWith ( "/output/dir" ) ;
103
105
} ) ;
104
106
105
107
/**
@@ -108,10 +110,10 @@ describe("cmdList", () => {
108
110
*/
109
111
test ( "calls list with undefined when no out-dir specified" , ( ) => {
110
112
const mockList = list as jest . MockedFunction < typeof list > ;
111
-
113
+
112
114
cmdList ( [ ] ) ;
113
-
114
- expect ( mockList ) . toHaveBeenCalledWith ( { } ) ;
115
+
116
+ expect ( mockList ) . toHaveBeenCalledWith ( undefined ) ;
115
117
} ) ;
116
118
117
119
/**
@@ -120,9 +122,11 @@ describe("cmdList", () => {
120
122
*/
121
123
test ( "handles unknown options gracefully" , ( ) => {
122
124
const mockPrintAndExit = utils . printAndExit as jest . MockedFunction < typeof utils . printAndExit > ;
123
-
125
+
124
126
cmdList ( [ "--unknown-flag" ] ) ;
125
-
126
- expect ( mockPrintAndExit ) . toHaveBeenCalledWith ( expect . stringContaining ( "--unknown-flag" ) ) ;
127
+
128
+ expect ( mockPrintAndExit ) . toHaveBeenCalledWith (
129
+ expect . stringContaining ( "--unknown-flag" )
130
+ ) ;
127
131
} ) ;
128
132
} ) ;
0 commit comments