Backend template with Core SDK and Web API.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

65 linhas
1.8 KiB

  1. import {describe, it, expect, vi, beforeAll, afterAll} from 'vitest';
  2. import { createCli } from '../src/cli';
  3. import { addCommands } from '../src/commands';
  4. import yargs from 'yargs';
  5. import { AdderServiceImpl, InvalidArgumentTypeError, ArgumentOutOfRangeError } from '../src/modules/adder';
  6. vi.mock('process');
  7. describe('blah', () => {
  8. let cli: yargs.Argv;
  9. let exit: vi.Mock<typeof process.exit>;
  10. let exitReal: typeof process.exit;
  11. beforeAll(() => {
  12. exitReal = process.exit.bind(process);
  13. const processMut = process as unknown as Record<string, unknown>;
  14. processMut.exit = exit = vi.fn();
  15. });
  16. afterAll(() => {
  17. process.exit = exitReal = process.exit.bind(process);
  18. });
  19. it('returns result when successful', async () => {
  20. cli = createCli(['add', '1', '2']);
  21. addCommands(cli);
  22. await cli.parse();
  23. expect(exit).toHaveBeenCalledWith(0);
  24. });
  25. it('returns error when given invalid inputs', async () => {
  26. vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
  27. throw new InvalidArgumentTypeError('Invalid input');
  28. });
  29. cli = createCli(['add', '1', '2']);
  30. addCommands(cli);
  31. await cli.parse();
  32. expect(exit).toHaveBeenCalledWith(-1);
  33. });
  34. it('returns error when given out-of-range inputs', async () => {
  35. vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
  36. throw new ArgumentOutOfRangeError('Out of range');
  37. });
  38. cli = createCli(['add', '1', '2']);
  39. addCommands(cli);
  40. await cli.parse();
  41. expect(exit).toHaveBeenCalledWith(-2);
  42. });
  43. it('returns error when an unexpected error occurs', async () => {
  44. vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
  45. throw new Error('Unexpected error');
  46. });
  47. cli = createCli(['add', '1', '2']);
  48. addCommands(cli);
  49. await cli.parse();
  50. expect(exit).toHaveBeenCalledWith(-2);
  51. });
  52. });