diff --git a/src/cli/cliRun.ts b/src/cli/cliRun.ts index d481e357..a07b571f 100644 --- a/src/cli/cliRun.ts +++ b/src/cli/cliRun.ts @@ -140,6 +140,7 @@ export const run = async () => { await program.parseAsync(process.argv); } catch (error) { handleError(error); + process.exit(1); } }; diff --git a/tests/cli/cliRun.test.ts b/tests/cli/cliRun.test.ts index f8da7177..d400df06 100644 --- a/tests/cli/cliRun.test.ts +++ b/tests/cli/cliRun.test.ts @@ -1,3 +1,4 @@ +import { program } from 'commander'; import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; import * as defaultAction from '../../src/cli/actions/defaultAction.js'; import * as initAction from '../../src/cli/actions/initAction.js'; @@ -36,16 +37,6 @@ vi.mock('../../src/shared/logger', () => ({ setLogLevelByEnv: vi.fn(), })); -vi.mock('commander', () => ({ - program: { - description: vi.fn().mockReturnThis(), - arguments: vi.fn().mockReturnThis(), - option: vi.fn().mockReturnThis(), - action: vi.fn().mockReturnThis(), - parseAsync: vi.fn().mockResolvedValue(undefined), - }, -})); - vi.mock('../../src/cli/actions/defaultAction'); vi.mock('../../src/cli/actions/initAction'); vi.mock('../../src/cli/actions/remoteAction'); @@ -163,8 +154,18 @@ describe('cliRun', () => { vi.mocked(versionAction.runVersionAction).mockResolvedValue(); }); - test('should run without arguments', async () => { + test('should call process.exit(1) on error', async () => { + const exitSpy = vi.spyOn(process, 'exit').mockImplementationOnce(() => undefined as never); + const parseSpy = vi.spyOn(program, 'description').mockImplementationOnce(() => { + throw Error(); + }); + const handleErrorSpy = vi.spyOn(logger, 'error'); await expect(run()).resolves.not.toThrow(); + expect(exitSpy).toHaveBeenCalledWith(1); + expect(handleErrorSpy).toHaveBeenCalled(); + exitSpy.mockReset(); + parseSpy.mockReset(); + handleErrorSpy.mockReset(); }); describe('executeAction', () => {