Backend template with Core SDK and Web API.

index.test.ts 1.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { describe, it, expect, vi, beforeAll } from 'vitest';
  2. import { Cli, createCli } from '../src/cli';
  3. import { addCommands } from '../src/commands';
  4. import { AdderServiceImpl, InvalidArgumentTypeError, ArgumentOutOfRangeError } from '../src/modules/adder';
  5. vi.mock('process');
  6. describe('blah', () => {
  7. let cli: Cli;
  8. beforeAll(() => {
  9. cli = createCli({
  10. name: 'cli-test',
  11. });
  12. addCommands(cli);
  13. });
  14. it('returns result when successful', async () => {
  15. const response = await cli.test().run(['add', '1', '2']);
  16. expect(response.exitCode).toBe(0);
  17. });
  18. it('returns error when given invalid inputs', async () => {
  19. vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
  20. throw new InvalidArgumentTypeError('Invalid input');
  21. });
  22. const response = await cli.test().run(['add', '1', '2']);
  23. expect(response.exitCode).toBe(-1);
  24. });
  25. it('returns error when given out-of-range inputs', async () => {
  26. vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
  27. throw new ArgumentOutOfRangeError('Out of range');
  28. });
  29. const response = await cli.test().run(['add', '1', '2']);
  30. expect(response.exitCode).toBe(-2);
  31. });
  32. it('returns error when an unexpected error occurs', async () => {
  33. vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
  34. throw new Error('Unexpected error');
  35. });
  36. const response = await cli.test().run(['add', '1', '2']);
  37. expect(response.exitCode).toBe(-3);
  38. });
  39. it('returns error when given insufficient arguments', async () => {
  40. const response = await cli.test().run(['add', '1']);
  41. expect(response.exitCode).toBe(-1);
  42. });
  43. describe('on interactive mode', () => {
  44. it('prompts values for insufficient arguments', async () => {
  45. const response = await cli
  46. .test()
  47. .promptValue({
  48. a: '1',
  49. b: '2',
  50. })
  51. .run(['add', '-i']);
  52. expect(response.exitCode).not.toBe(-1);
  53. });
  54. });
  55. });