您没有从异步函数返回结果。
异步fat arrow函数是用大括号定义的,这意味着它需要显式的return语句来返回值。
// Curly braces requires explicit return
() => {}
// This doesn't return anything because there is no `return` statement
() => {
5;
}
// This returns a number
() => {
return 5;
}
// This fat arrow syntax has an implicit return
() => ()
// These have an implicit return and are equivalent
() => 5
() => (5)
您可以使用显式返回:
// Use explicit return
it.each([null, undefined, []])('should throw Http exception with expected exception message', async (n) => {
return await expect(() => service.check(n)).rejects.toThrow(
new HttpException(
{
statusCode: HttpStatus.BAD_REQUEST,
message: "Nothing to check",
error: HttpStatus.BAD_REQUEST,
},
HttpStatus.BAD_REQUEST,
),
);
});
或者去掉花括号。
在第二种情况下,我删除了大括号,并将函数体包装在圆括号中,以使分组更加明确。
// Use implicit return - note curly braces were changed to round brackets
it.each([null, undefined, []])('should throw Http exception with expected exception message', async (n) => (
await expect(() => service.check(n)).rejects.toThrow(
new HttpException(
{
statusCode: HttpStatus.BAD_REQUEST,
message: "Nothing to check",
error: HttpStatus.BAD_REQUEST,
},
HttpStatus.BAD_REQUEST,
),
);
));