我如何测试(开玩笑)以期望返回?

I'l*_*ack 6 javascript node.js jestjs

如何使用 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;
  }
};
Run Code Online (Sandbox Code Playgroud)

我能够测试控制台日志,但我也喜欢测试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');
  });
Run Code Online (Sandbox Code Playgroud)

art*_*ień 8

没有返回值的函数将返回“未定义”,因此您可以使用 not.toBeUndefine(); 进行测试。