@@ -4,11 +4,11 @@ import { runCLI } from './index';
44import printTableFromInp from './src/service' ;
55
66// Mock dependencies
7- jest . mock ( 'fs' ) ;
87jest . mock ( './src/service' ) ;
98jest . mock ( 'commander' , ( ) => {
109 const mockCommand = {
1110 option : jest . fn ( ) . mockReturnThis ( ) ,
11+ version : jest . fn ( ) . mockReturnThis ( ) ,
1212 parse : jest . fn ( ) . mockReturnThis ( ) ,
1313 opts : jest . fn ( ) . mockReturnValue ( { } ) ,
1414 } ;
@@ -23,18 +23,36 @@ describe('CLI', () => {
2323 mockConsoleLog = jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
2424
2525 // Reset mocks
26- ( fs . readFileSync as jest . Mock ) . mockReset ( ) ;
2726 ( printTableFromInp as jest . Mock ) . mockReset ( ) ;
2827
2928 // Reset Commander mock
3029 const mockCommand = new Command ( ) ;
3130 ( mockCommand . opts as jest . Mock ) . mockReturnValue ( { } ) ;
31+ ( mockCommand . version as jest . Mock ) . mockReset ( ) ;
32+ ( mockCommand . option as jest . Mock ) . mockReset ( ) ;
33+ ( mockCommand . parse as jest . Mock ) . mockReset ( ) ;
34+
35+ // Set up default mock returns
36+ ( mockCommand . version as jest . Mock ) . mockReturnValue ( mockCommand ) ;
37+ ( mockCommand . option as jest . Mock ) . mockReturnValue ( mockCommand ) ;
38+ ( mockCommand . parse as jest . Mock ) . mockReturnValue ( mockCommand ) ;
3239 } ) ;
3340
3441 afterEach ( ( ) => {
3542 jest . restoreAllMocks ( ) ;
3643 } ) ;
3744
45+ it ( 'should configure version command with package version' , ( ) => {
46+ const mockCommand = new Command ( ) ;
47+ runCLI ( [ 'node' , 'index.js' ] ) ;
48+
49+ expect ( mockCommand . version ) . toHaveBeenCalledWith (
50+ expect . any ( String ) ,
51+ '-v, --version' ,
52+ 'output the current version'
53+ ) ;
54+ } ) ;
55+
3856 it ( 'should handle input option' , ( ) => {
3957 const input = '[{"id":1,"name":"John"}]' ;
4058 const mockCommand = new Command ( ) ;
@@ -60,12 +78,17 @@ describe('CLI', () => {
6078 const input = '[{"id":1,"name":"John"}]' ;
6179 const mockCommand = new Command ( ) ;
6280 ( mockCommand . opts as jest . Mock ) . mockReturnValue ( { stdin : true } ) ;
63- ( fs . readFileSync as jest . Mock ) . mockReturnValue ( { toString : ( ) => input } ) ;
81+
82+ // Mock fs.readFileSync only for this test
83+ const mockReadFileSync = jest . spyOn ( fs , 'readFileSync' )
84+ . mockReturnValue ( Buffer . from ( input ) ) ;
6485
6586 runCLI ( [ 'node' , 'index.js' , '-s' ] ) ;
6687
67- expect ( fs . readFileSync ) . toHaveBeenCalledWith ( 0 ) ;
88+ expect ( mockReadFileSync ) . toHaveBeenCalledWith ( 0 ) ;
6889 expect ( printTableFromInp ) . toHaveBeenCalledWith ( input , undefined ) ;
90+
91+ mockReadFileSync . mockRestore ( ) ;
6992 } ) ;
7093
7194 it ( 'should show error when no input option is provided' , ( ) => {
0 commit comments