|
- import { describe, it, expect, vi, beforeAll } from 'vitest';
- import { Cli, createCli } from '../src/cli';
- import { addCommands } from '../src/commands';
- import { AdderServiceImpl, InvalidArgumentTypeError, ArgumentOutOfRangeError } from '../src/modules/adder';
-
- vi.mock('process');
-
- describe('blah', () => {
- let cli: Cli;
- beforeAll(() => {
- cli = createCli({
- name: 'cli-test',
- });
- 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);
- });
- });
- });
|