|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- import { describe, it, expect, vi, beforeAll } from 'vitest';
- import { createCli } from '../src/cli';
- import { addCommands } from '../src/commands';
- import { AdderServiceImpl, InvalidArgumentTypeError, ArgumentOutOfRangeError } from '../src/modules/adder';
- import { Cli } from '../src/packages/cli-wrapper';
-
- vi.mock('process');
-
- describe('cli', () => {
- let cli: Cli;
- beforeAll(() => {
- cli = createCli({
- name: 'cli-test',
- logger: false,
- });
- addCommands(cli);
- });
-
- it('returns result when successful', async () => {
- const response = await cli.test().run(['add', '1', '2']);
- expect(response.exitCode).toBe(0);
- });
-
- it('returns error when given invalid inputs', async () => {
- vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
- throw new InvalidArgumentTypeError('Invalid input');
- });
-
- const response = await cli.test().run(['add', '1', '2']);
- expect(response.exitCode).toBe(-1);
- });
-
- it('returns error when given out-of-range inputs', async () => {
- vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
- throw new ArgumentOutOfRangeError('Out of range');
- });
-
- const response = await cli.test().run(['add', '1', '2']);
- expect(response.exitCode).toBe(-2);
- });
-
- it('returns error when an unexpected error occurs', async () => {
- vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
- throw new Error('Unexpected error');
- });
-
- const response = await cli.test().run(['add', '1', '2']);
- expect(response.exitCode).toBe(-3);
- });
-
- it('returns error when given insufficient arguments', async () => {
- const response = await cli.test().run(['add', '1']);
- expect(response.exitCode).toBe(-1);
- });
-
- describe('on interactive mode', () => {
- it('prompts values for insufficient arguments', async () => {
- const response = await cli
- .test()
- .promptValue({
- a: '1',
- b: '2',
- })
- .run(['add', '-i']);
- expect(response.exitCode).not.toBe(-1);
- });
- });
- });
|