如何解决在 NodeJS 应用程序上进行 Jest 测试时期望错误处理
我想测试我的 KoaJS 控制器。我的控制器就是这样:
public static async myController (ctx: Context): Promise<void> {
var body = {};
var status = 406;
const var1 = ctx.request.body.var1;
if (var1 !== 0) {
const error = new CustomException('title','message',10)
throw error
}
status = 200;
body = var1;
ctx.status = status;
ctx.body = body;
}
我的测试是这样的:
test('Getting error by wrong var1 value',async () => {
const context = {status: undefined,body: undefined,request : {body: {"var1" : "123"}} } as Context;
await CustomController.myController(context);
expect(context).toThrow(CustomException)
});
});
所以当我运行测试时,我收到了这样的错误:
53 | if (var1 !== 0) {
54 |
> 55 | const error = new CustomException('title',10)
| ^
56 | throw error
57 | }
A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks.
我是测试新手。我花了很多时间来解决这个错误。我怎么解决这个问题?为什么我收到这个错误?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。