Best Way to Test Promises in Jest

后端 未结 3 947
一向
一向 2020-12-11 00:32

Unless I\'m misunderstanding something, the resolves and rejects (https://facebook.github.io/jest/docs/expect.html#resolves) won\'t be available until vNext. What is the rec

3条回答
  •  心在旅途
    2020-12-11 00:51

    I was able to test JEST with AXIOS for HTTP REST calls like this.

    it('has an API worth testing', async () => {
      let httpResult = null;
      await callThefunctionThatReturnsPromiseToMakeTheAxiosApiCall()
        .then(function(result) {httpResult=result;})
        .catch(function(err) {httpResult=err;});
      expect(httpResult.data.myData).toBe("myExpectedValue");
    });
    

    or

    it('has an API worth testing', async () => {
      let httpResult = await callThefunctionThatReturnsPromiseToMakeTheAxiosApiCall();
      expect(httpResult.data.myData).toBe("myExpectedValue");
    });
    

提交回复
热议问题