[英]How do I test (jest) to expect return?
使用 Jest 我如何测试return
例如使用 Lambda:
exports.handler = async (event, context, callback) => {
try {
const orders = await getOrders();
if (!orders) {
console.log("There is no Orders");
return; //<< How do test this?
}
// Something else
} catch (err) {
throw new;
}
};
我能够测试控制台日志,但我也想测试期望return
目前我在测试中使用它:
it("should terminate if there is no order", async () => {
console.log = jest.fn();
let order = await func.handler();
expect(console.log.mock.calls[0][0]).toBe('There is no Orders');
});
没有返回的函数将返回 'undefined',因此您可以使用 not.toBeUndefined(); 测试它。
简单地期望值是未定义的:
expect(order).toBeUndefined();
Necroposting here,但开玩笑现在必须toReturn
和toReturnWith
,相应地没有和有 arguments 评估。 还有toHaveReturned
和toHaveReturnedWith
以适应新语言。
您可以在handler
的spyOn
变体上使用它:
it("should terminate if there is no order", async () => {
jest.spyOn(func, "handler")
console.log = jest.fn();
let order = await func.handler();
expect(func.handler).toHaveReturned();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.