File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed
test/unit/commands/module Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -147,4 +147,23 @@ describe('ModuleInitCommand Unit', () => {
147147 expect ( fs . writeFile ) . not . toHaveBeenCalled ( ) ;
148148 expect ( logger . debug ) . not . toHaveBeenCalledWith ( expect . stringContaining ( 'Updated' ) ) ;
149149 } ) ;
150+
151+ it ( 'should skip cleanup if target directory does not exist after failure' , async ( ) => {
152+ // Mock early failure in ensureDir to trigger catch block
153+ vi . mocked ( fs . ensureDir ) . mockRejectedValue ( new Error ( 'Setup failed' ) ) ;
154+ // Mock target dir validation passing (initially does not exist)
155+ // Then inside catch block, it checks existence again
156+ // We want it to be false so it SKIPS fs.remove
157+ // We use mockResolvedValueOnce chaining if needed, but here pathExists is called:
158+ // 1. Validation (line 37): needs to be false (or true but empty, but let's say false for new module)
159+ // 2. Cleanup check (line 76): needs to be false
160+ vi . mocked ( fs . pathExists ) . mockResolvedValue ( false as never ) ;
161+
162+ const errorSpy = vi . spyOn ( command , 'error' ) . mockImplementation ( ( ) => { } ) ;
163+
164+ await expect ( command . run ( { module_name : 'test-module' } ) ) . rejects . toThrow ( 'process.exit' ) ;
165+
166+ expect ( errorSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Failed to initialize module' ) ) ;
167+ expect ( fs . remove ) . not . toHaveBeenCalled ( ) ;
168+ } ) ;
150169} ) ;
You can’t perform that action at this time.
0 commit comments