简体   繁体   English

Jest 承诺返回未定义

[英]Jest promise returns undefined

My code:我的代码:

this.apiStore.fetchOrder(id).then(() => {
  alert("Success");
  // TODO - ui updates
})

Jest test:玩笑测试:

  describe("order", () => {
    it("fetches the order", () => {
      const fetchOrder = jest.fn();
      const id = 100;
      expect(fetchOrder).toHaveBeenCalledWith(id);
    });
  });

But I am getting the following error:但我收到以下错误:

Cannot read property 'then' of undefined无法读取未定义的属性“then”

Any idea on how to fix this?关于如何解决这个问题的任何想法?

Edit 1:编辑1:

Fetchorder api call is defined as follows Fetchorder api调用定义如下

fetchOrder = (orderId) => {
    return this.fetch(
      `/api/order.json`,
      {
        method: "POST",
        body: {
          id: orderId
        },
      }
    );
  };

For now i am returning the response as现在我将响应返回为

{"status":"ok"}

if i remove the .then() => {..} then the test is running fine.如果我删除.then() => {..}则测试运行良好。 Any ideas?有任何想法吗?

Edit 2: The backend code is as follows编辑2:后端代码如下

def fetch_order
  render json: { status: :ok }
end

fetchOrder 函数不返回 Promise 实例,您应该检查其实现或共享它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM