|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import {describe, it, expect, vi, beforeAll, afterAll} from 'vitest';
-
- import { createCli } from '../src/cli';
- import { addCommands } from '../src/commands';
- import yargs from 'yargs';
- import { AdderServiceImpl, InvalidArgumentTypeError, ArgumentOutOfRangeError } from '../src/modules/adder';
-
- vi.mock('process');
-
- describe('blah', () => {
- let cli: yargs.Argv;
- let exit: vi.Mock<typeof process.exit>;
- let exitReal: typeof process.exit;
-
- beforeAll(() => {
- exitReal = process.exit.bind(process);
- const processMut = process as unknown as Record<string, unknown>;
- processMut.exit = exit = vi.fn();
- });
-
- afterAll(() => {
- process.exit = exitReal = process.exit.bind(process);
- });
-
- it('returns result when successful', async () => {
- cli = createCli(['add', '1', '2']);
- addCommands(cli);
- await cli.parse();
- expect(exit).toHaveBeenCalledWith(0);
- });
-
- it('returns error when given invalid inputs', async () => {
- vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
- throw new InvalidArgumentTypeError('Invalid input');
- });
-
- cli = createCli(['add', '1', '2']);
- addCommands(cli);
- await cli.parse();
- expect(exit).toHaveBeenCalledWith(-1);
- });
-
- it('returns error when given out-of-range inputs', async () => {
- vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
- throw new ArgumentOutOfRangeError('Out of range');
- });
-
- cli = createCli(['add', '1', '2']);
- addCommands(cli);
- await cli.parse();
- expect(exit).toHaveBeenCalledWith(-2);
- });
-
- it('returns error when an unexpected error occurs', async () => {
- vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
- throw new Error('Unexpected error');
- });
-
- cli = createCli(['add', '1', '2']);
- addCommands(cli);
- await cli.parse();
- expect(exit).toHaveBeenCalledWith(-2);
- });
- });
|