Files
nest/packages/platform-fastify/test/adapters/fastify-adapter.spec.ts

50 lines
1.6 KiB
TypeScript

import { FastifyAdapter } from '../../adapters/fastify-adapter';
import { createError } from '@fastify/error';
import { HttpException } from '@nestjs/common';
describe('FastifyAdapter', () => {
let fastifyAdapter: FastifyAdapter;
beforeEach(() => {
fastifyAdapter = new FastifyAdapter();
});
afterEach(() => vi.restoreAllMocks());
describe('mapException', () => {
it('should map FastifyError with status code to HttpException', () => {
const FastifyErrorCls = createError(
'FST_ERR_CTP_INVALID_MEDIA_TYPE',
'Unsupported Media Type: %s',
415,
);
const error = new FastifyErrorCls();
const result = fastifyAdapter.mapException(error) as HttpException;
expect(result).to.be.instanceOf(HttpException);
expect(result.message).to.equal(error.message);
expect(result.getStatus()).to.equal(415);
});
it('should return FastifyError without user status code to Internal Server Error HttpException', () => {
const FastifyErrorCls = createError(
'FST_WITHOUT_STATUS_CODE',
'Error without status code',
);
const error = new FastifyErrorCls();
const result = fastifyAdapter.mapException(error) as HttpException;
expect(result).to.be.instanceOf(HttpException);
expect(result.message).to.equal(error.message);
expect(result.getStatus()).to.equal(500);
});
it('should return error if it is not FastifyError', () => {
const error = new Error('Test error');
const result = fastifyAdapter.mapException(error);
expect(result).to.equal(error);
});
});
});