Backend template with Core SDK and Web API.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

index.test.ts 2.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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('Example', () => {
  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('/')
  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('/')
  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('/')
  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('/')
  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. });