Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for 422 HTTP Error #1468

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions test/functional/express-error-handling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import express from 'express';
import { Server as HttpServer } from 'http';
import HttpStatusCodes from 'http-status-codes';
import { Get } from '../../src/decorator/Get';
import { Post } from '../../src/decorator/Post';
import { JsonController } from '../../src/decorator/JsonController';
import { Middleware } from '../../src/decorator/Middleware';
import { UseAfter } from '../../src/decorator/UseAfter';
import { BodyParam } from '../../src/decorator/BodyParam';
import { ExpressErrorMiddlewareInterface } from '../../src/driver/express/ExpressErrorMiddlewareInterface';
import { HttpError } from '../../src/http-error/HttpError';
import { NotFoundError } from '../../src/http-error/NotFoundError';
import { UnprocessableEntityError } from '../../src/http-error/UnprocessableEntityError';
import { createExpressServer, getMetadataArgsStorage } from '../../src/index';
import { axios } from '../utilities/axios';
import DoneCallback = jest.DoneCallback;
Expand Down Expand Up @@ -111,6 +114,11 @@ describe(``, () => {
stories(): never {
throw new ToJsonError(503, 'sorry, try it again later', 'impatient user');
}

@Post('/videos')
createVideo(@BodyParam('meta') meta: string): never {
throw new UnprocessableEntityError('Meta is an array of strings.');
}
}

expressServer = createExpressServer().listen(3001, done);
Expand Down Expand Up @@ -182,5 +190,17 @@ describe(``, () => {
expect(error.response.data.secretData).toBeUndefined();
}
});

it('should call global error handler middleware for validation params', async () => {
expect.assertions(3);

try {
await axios.post('/videos', { meta: 'new' });
} catch (error) {
expect(errorHandlerCalled).toBeTruthy();
expect(error.response.status).toEqual(HttpStatusCodes.UNPROCESSABLE_ENTITY);
expect(error.response.data.message).toEqual('Meta is an array of strings.');
}
});
});
});
Loading