Sinon JS测试异步功能

时间:2019-01-09 13:17:59

标签: jestjs sinon nock

我正在尝试测试包含异步调用的函数。我尝试使用setTimeout和sinon.useFakeTimers();,但效果不佳。

第一个异步调用是使用axios模拟的nock进行的api调用。但是仍然可以在执行all函数之前完成测试。

这是我的考试

describe("endpoint request", () => {
  beforeEach(() => {
    nock("http://endpoint.com")
      .get("/params")
      .reply(200, response);
  });

  it("should return a receipt", function() {
    let req = httpMocks.createRequest({
      params: {
        locale: "uk-en",
        surname: "presley"
      }
    });
    const spy = (res.writeHead().end = sinon.spy());
    getPNGReceipt(req, res);
    expect(spy.calledOnce).toEqual(true);
  });
});

getPNGReceipt内,我向http://endpoint.com/params发出了get请求,但在expect(spy.calledOnce).toEqual(true);完成之前调用了getPNGReceipt(req, res);

如何在执行sinon之前让expect(spy.calledOnce).toEqual(true);等待?

0 个答案:

没有答案
相关问题