Backend template with Core SDK and Web API.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

158 строки
3.8 KiB

  1. import { FastifyInstance } from 'fastify';
  2. import {
  3. describe,
  4. it,
  5. expect,
  6. beforeAll,
  7. afterAll,
  8. vi,
  9. } from 'vitest';
  10. import { constants } from 'http2';
  11. import { createServer } from '../src/server';
  12. import { addRoutes } from '../src/routes';
  13. import {
  14. AdderServiceImpl,
  15. ArgumentOutOfRangeError,
  16. InvalidArgumentTypeError,
  17. } from '../src/modules/adder';
  18. describe('add', () => {
  19. let server: FastifyInstance;
  20. const body = { a: 1, b: 2 };
  21. beforeAll(() => {
  22. server = createServer();
  23. addRoutes(server);
  24. });
  25. afterAll(async () => {
  26. await server.close();
  27. });
  28. it('returns result when successful', async () => {
  29. const response = await server
  30. .inject()
  31. .post('/add')
  32. .body(body)
  33. .headers({
  34. 'Accept': 'application/json',
  35. });
  36. expect(response.statusCode).toBe(constants.HTTP_STATUS_OK);
  37. });
  38. it('returns error when given invalid inputs', async () => {
  39. vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
  40. throw new InvalidArgumentTypeError('Invalid input');
  41. });
  42. const response = await server
  43. .inject()
  44. .post('/add')
  45. .body(body)
  46. .headers({
  47. 'Accept': 'application/json',
  48. });
  49. expect(response.statusCode).toBe(constants.HTTP_STATUS_BAD_REQUEST);
  50. });
  51. it('returns error when given out-of-range inputs', async () => {
  52. vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
  53. throw new ArgumentOutOfRangeError('Out of range');
  54. });
  55. const response = await server
  56. .inject()
  57. .post('/add')
  58. .body(body)
  59. .headers({
  60. 'Accept': 'application/json',
  61. });
  62. expect(response.statusCode).toBe(constants.HTTP_STATUS_BAD_REQUEST);
  63. });
  64. it('returns error when an unexpected error occurs', async () => {
  65. vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
  66. throw new Error('Unexpected error');
  67. });
  68. const response = await server
  69. .inject()
  70. .post('/add')
  71. .body({ a: 1, b: 2 })
  72. .headers({
  73. 'Accept': 'application/json',
  74. });
  75. expect(response.statusCode).toBe(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR);
  76. });
  77. });
  78. describe('subtract', () => {
  79. let server: FastifyInstance;
  80. const body = { a: 1, b: 2 };
  81. beforeAll(() => {
  82. server = createServer();
  83. addRoutes(server);
  84. });
  85. afterAll(async () => {
  86. await server.close();
  87. });
  88. it('returns result when successful', async () => {
  89. const response = await server
  90. .inject()
  91. .post('/subtract')
  92. .body(body)
  93. .headers({
  94. 'Accept': 'application/json',
  95. });
  96. expect(response.statusCode).toBe(constants.HTTP_STATUS_OK);
  97. });
  98. it('returns error when given invalid inputs', async () => {
  99. vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
  100. throw new InvalidArgumentTypeError('Invalid input');
  101. });
  102. const response = await server
  103. .inject()
  104. .post('/subtract')
  105. .body(body)
  106. .headers({
  107. 'Accept': 'application/json',
  108. });
  109. expect(response.statusCode).toBe(constants.HTTP_STATUS_BAD_REQUEST);
  110. });
  111. it('returns error when given out-of-range inputs', async () => {
  112. vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
  113. throw new ArgumentOutOfRangeError('Out of range');
  114. });
  115. const response = await server
  116. .inject()
  117. .post('/subtract')
  118. .body(body)
  119. .headers({
  120. 'Accept': 'application/json',
  121. });
  122. expect(response.statusCode).toBe(constants.HTTP_STATUS_BAD_REQUEST);
  123. });
  124. it('returns error when an unexpected error occurs', async () => {
  125. vi.spyOn(AdderServiceImpl.prototype, 'addNumbers').mockImplementationOnce(() => {
  126. throw new Error('Unexpected error');
  127. });
  128. const response = await server
  129. .inject()
  130. .post('/subtract')
  131. .body({ a: 1, b: 2 })
  132. .headers({
  133. 'Accept': 'application/json',
  134. });
  135. expect(response.statusCode).toBe(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR);
  136. });
  137. });