Skip to content

Commit 3e899ad

Browse files
committed
test: koa error
1 parent e9f091a commit 3e899ad

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/test/test-koa.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ describe('ERest Koa Integration', () => {
2929
describe('forceGroup: false', () => {
3030
const app = new Koa();
3131
app.use(bodyParser()) // Use bodyParser for all non-group tests
32+
app.use(async (ctx, next) => {
33+
try {
34+
await next();
35+
} catch (err: any) {
36+
ctx.status = err.status || 500;
37+
ctx.body = JSON.stringify({
38+
message: err.message,
39+
stack: process.env.NODE_ENV === 'development' ? err.stack : undefined,
40+
});
41+
}
42+
});
3243
const apiService = lib();
3344
const router = new KoaRouter();
3445
server = app.listen()
@@ -63,24 +74,23 @@ describe('ERest Koa Integration', () => {
6374
expect(ret).toStrictEqual({ data: 'koa works' });
6475
});
6576
it('should validate query parameters (execution)', async () => {
66-
const ret1 = await apiService.test.get('/query-test').query({ name: "tester" }).success();
67-
expect(ret1).toStrictEqual({ name: 'tester' });
77+
const ret = await apiService.test.get('/query-test').query({ name: "tester" }).success();
78+
expect(ret).toStrictEqual({ name: 'tester' });
79+
});
80+
it('should validate POST body parameters (success)', async () => {
81+
const ret = await apiService.test.post('/body-test').input({ id: 'abc' }).error();
82+
expect(ret).toStrictEqual(new Error('POST_/body-test 期望API输出失败结果,但实际输出成功结果:{}'));
6883
});
69-
it('should validate POST body parameters (execution)', async () => {
84+
85+
it('should validate POST body parameters (error)', async () => {
7086
const ret1 = await apiService.test.post('/body-test').input({ id: 123 }).success();
7187
expect(ret1).toStrictEqual({ id: 123 });
72-
const ret2 = await apiService.test.post('/body-test').input({ id: 'abc' }).error();
73-
console.error(ret2);
74-
expect(ret2).toStrictEqual(new Error('POST_/body-test 期望API输出失败结果,但实际输出成功结果:{}'));
7588
});
7689

90+
7791
});
7892

7993
describe('forceGroup: true', () => {
80-
let server: any;
81-
afterAll(() => {
82-
server.close();
83-
});
8494
const appGroup = new Koa();
8595
appGroup.use(bodyParser());
8696
const erestGroup = setupERestWithGroup();

0 commit comments

Comments
 (0)