|
| 1 | +import { FastifyAdapter } from '../../adapters/fastify-adapter'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { createError } from '@fastify/error'; |
| 4 | +import { HttpException } from '@nestjs/common'; |
| 5 | + |
| 6 | +describe('FastifyAdapter', () => { |
| 7 | + let fastifyAdapter: FastifyAdapter; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + fastifyAdapter = new FastifyAdapter(); |
| 11 | + }); |
| 12 | + |
| 13 | + describe('mapException', () => { |
| 14 | + it('should map FastifyError with status code to HttpException', () => { |
| 15 | + const FastifyErrorCls = createError( |
| 16 | + 'FST_ERR_CTP_INVALID_MEDIA_TYPE', |
| 17 | + 'Unsupported Media Type: %s', |
| 18 | + 415, |
| 19 | + ); |
| 20 | + const error = new FastifyErrorCls(); |
| 21 | + |
| 22 | + const result = fastifyAdapter.mapException(error) as HttpException; |
| 23 | + |
| 24 | + expect(result).to.be.instanceOf(HttpException); |
| 25 | + expect(result.message).to.equal(error.message); |
| 26 | + expect(result.getStatus()).to.equal(415); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should return FastifyError without user status code to Internal Server Error HttpException', () => { |
| 30 | + const FastifyErrorCls = createError( |
| 31 | + 'FST_WITHOUT_STATUS_CODE', |
| 32 | + 'Error without status code', |
| 33 | + ); |
| 34 | + const error = new FastifyErrorCls(); |
| 35 | + |
| 36 | + const result = fastifyAdapter.mapException(error) as HttpException; |
| 37 | + expect(result).to.be.instanceOf(HttpException); |
| 38 | + expect(result.message).to.equal(error.message); |
| 39 | + expect(result.getStatus()).to.equal(500); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return error if it is not FastifyError', () => { |
| 43 | + const error = new Error('Test error'); |
| 44 | + const result = fastifyAdapter.mapException(error); |
| 45 | + expect(result).to.equal(error); |
| 46 | + }); |
| 47 | + }); |
| 48 | +}); |
0 commit comments