Skip to content

Commit 959be82

Browse files
chore: test for 422 HTTP Error (#1468)
1 parent 6483625 commit 959be82

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

test/functional/express-error-handling.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import express from 'express';
22
import { Server as HttpServer } from 'http';
33
import HttpStatusCodes from 'http-status-codes';
44
import { Get } from '../../src/decorator/Get';
5+
import { Post } from '../../src/decorator/Post';
56
import { JsonController } from '../../src/decorator/JsonController';
67
import { Middleware } from '../../src/decorator/Middleware';
78
import { UseAfter } from '../../src/decorator/UseAfter';
9+
import { BodyParam } from '../../src/decorator/BodyParam';
810
import { ExpressErrorMiddlewareInterface } from '../../src/driver/express/ExpressErrorMiddlewareInterface';
911
import { HttpError } from '../../src/http-error/HttpError';
1012
import { NotFoundError } from '../../src/http-error/NotFoundError';
13+
import { UnprocessableEntityError } from '../../src/http-error/UnprocessableEntityError';
1114
import { createExpressServer, getMetadataArgsStorage } from '../../src/index';
1215
import { axios } from '../utilities/axios';
1316
import DoneCallback = jest.DoneCallback;
@@ -111,6 +114,11 @@ describe(``, () => {
111114
stories(): never {
112115
throw new ToJsonError(503, 'sorry, try it again later', 'impatient user');
113116
}
117+
118+
@Post('/videos')
119+
createVideo(@BodyParam('meta') meta: string): never {
120+
throw new UnprocessableEntityError('Meta is an array of strings.');
121+
}
114122
}
115123

116124
expressServer = createExpressServer().listen(3001, done);
@@ -182,5 +190,17 @@ describe(``, () => {
182190
expect(error.response.data.secretData).toBeUndefined();
183191
}
184192
});
193+
194+
it('should call global error handler middleware for validation params', async () => {
195+
expect.assertions(3);
196+
197+
try {
198+
await axios.post('/videos', { meta: 'new' });
199+
} catch (error) {
200+
expect(errorHandlerCalled).toBeTruthy();
201+
expect(error.response.status).toEqual(HttpStatusCodes.UNPROCESSABLE_ENTITY);
202+
expect(error.response.data.message).toEqual('Meta is an array of strings.');
203+
}
204+
});
185205
});
186206
});

0 commit comments

Comments
 (0)