fix: update tests

This commit is contained in:
glebbash
2026-02-17 12:18:53 +00:00
parent 4ec9445776
commit 4aef438088

View File

@@ -1,14 +1,12 @@
import { HttpException } from '@nestjs/common';
import { isNil, isObject } from '@nestjs/common/utils/shared.utils';
import { expect } from 'chai';
import * as createHttpError from 'http-errors';
import * as sinon from 'sinon';
import { isNil, isObject } from '@nestjs/common/utils/shared.utils.js';
import createHttpError from 'http-errors';
import { AbstractHttpAdapter } from '../../adapters/index.js';
import { InvalidExceptionFilterException } from '../../errors/exceptions/invalid-exception-filter.exception.js';
import { ExceptionsHandler } from '../../exceptions/exceptions-handler.js';
import { ExecutionContextHost } from '../../helpers/execution-context-host.js';
import { NoopHttpAdapter } from '../utils/noop-adapter.js';
import fastifyErrors from '@fastify/error';
import { AbstractHttpAdapter } from '../../adapters';
import { InvalidExceptionFilterException } from '../../errors/exceptions/invalid-exception-filter.exception';
import { ExceptionsHandler } from '../../exceptions/exceptions-handler';
import { ExecutionContextHost } from '../../helpers/execution-context-host';
import { NoopHttpAdapter } from '../utils/noop-adapter.spec';
describe('ExceptionsHandler', () => {
let adapter: AbstractHttpAdapter;
@@ -64,14 +62,12 @@ describe('ExceptionsHandler', () => {
)();
handler.next(fastifyError, new ExecutionContextHost([0, response]));
expect(statusStub.calledWith(400)).to.be.true;
expect(
jsonStub.calledWith({
statusCode: 400,
message:
"Body cannot be empty when content-type is set to 'application/json'",
}),
).to.be.true;
expect(statusStub).toHaveBeenCalledWith(400);
expect(jsonStub).toHaveBeenCalledWith({
statusCode: 400,
message:
"Body cannot be empty when content-type is set to 'application/json'",
});
});
it('should not treat errors from external API calls as errors from "http-errors" library', () => {
const apiCallError = Object.assign(
@@ -80,13 +76,11 @@ describe('ExceptionsHandler', () => {
);
handler.next(apiCallError, new ExecutionContextHost([0, response]));
expect(statusStub.calledWith(500)).to.be.true;
expect(
jsonStub.calledWith({
statusCode: 500,
message: 'Internal server error',
}),
).to.be.true;
expect(statusStub).toHaveBeenCalledWith(500);
expect(jsonStub).toHaveBeenCalledWith({
statusCode: 500,
message: 'Internal server error',
});
});
it('should treat fastify errors as http errors', () => {
const fastifyError = fastifyErrors.createError(
@@ -96,14 +90,12 @@ describe('ExceptionsHandler', () => {
)();
handler.next(fastifyError, new ExecutionContextHost([0, response]));
expect(statusStub.calledWith(400)).to.be.true;
expect(
jsonStub.calledWith({
statusCode: 400,
message:
"Body cannot be empty when content-type is set to 'application/json'",
}),
).to.be.true;
expect(statusStub).toHaveBeenCalledWith(400);
expect(jsonStub).toHaveBeenCalledWith({
statusCode: 400,
message:
"Body cannot be empty when content-type is set to 'application/json'",
});
});
it('should not treat errors from external API calls as errors from "http-errors" library', () => {
const apiCallError = Object.assign(
@@ -112,13 +104,11 @@ describe('ExceptionsHandler', () => {
);
handler.next(apiCallError, new ExecutionContextHost([0, response]));
expect(statusStub.calledWith(500)).to.be.true;
expect(
jsonStub.calledWith({
statusCode: 500,
message: 'Internal server error',
}),
).to.be.true;
expect(statusStub).toHaveBeenCalledWith(500);
expect(jsonStub).toHaveBeenCalledWith({
statusCode: 500,
message: 'Internal server error',
});
});
describe('when exception is instantiated by "http-errors" library', () => {
it('should send expected response status code and message', () => {