最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Test the status code of a real request to an API with Jest - Stack Overflow

programmeradmin2浏览0评论

Hello I'm trying to test this API call but I don't know how to test for the status code of the response since it is a real (and it has to stay like that) API call and not a mock one

this is the function I'm testing:

export const getDataFromApi = (url) => {
  return axios.get(url)
    .then(({ data }) => data)
    .catch(err => console.log(err.toString()));
} 

and this is the test:

describe('Read data from API', () => {
  test('Get result of the API call', (done) => {
    const apiUrl = "";
    getDataFromApi(apiUrl)
      .then(data => {
        expect(data).toBeDefined();
        expect(data.results.length).toBeGreaterThan(0);
        done();
      });
  });
});

how can I expect if the status code of data is 200 or if is another status code?

also is necessary for me to leave that done after the execution of the function? I know with call backs I have to put it but with this promise I'm not sure

Hello I'm trying to test this API call but I don't know how to test for the status code of the response since it is a real (and it has to stay like that) API call and not a mock one

this is the function I'm testing:

export const getDataFromApi = (url) => {
  return axios.get(url)
    .then(({ data }) => data)
    .catch(err => console.log(err.toString()));
} 

and this is the test:

describe('Read data from API', () => {
  test('Get result of the API call', (done) => {
    const apiUrl = "https://rickandmortyapi./api/character";
    getDataFromApi(apiUrl)
      .then(data => {
        expect(data).toBeDefined();
        expect(data.results.length).toBeGreaterThan(0);
        done();
      });
  });
});

how can I expect if the status code of data is 200 or if is another status code?

also is necessary for me to leave that done after the execution of the function? I know with call backs I have to put it but with this promise I'm not sure

Share Improve this question edited Nov 1, 2019 at 15:16 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Nov 1, 2019 at 13:57 Carlos DelgadoCarlos Delgado 1691 gold badge3 silver badges15 bronze badges 2
  • I didn't see the code assert the status code – Lin Du Commented Nov 4, 2019 at 4:12
  • the status code is what I want to test to check if I get 200 or other code @slideshowp2 – Carlos Delgado Commented Nov 4, 2019 at 18:15
Add a ment  | 

1 Answer 1

Reset to default 2

Axios has a single response object returned in both the success and error paths which contains the HTTP status code. An error is raised if the response is not in the 2xx range.

You can plumb the status code as a return object from your getDataFromApi() wrapper function, but you'll probably want the full response object for other checks (like headers). I remend getting rid of the wrapper altogether.

Without the wrapper, here's 2 different status checks using promises, one for success and one for failure:

describe('Read data from API', () => {
  test('Get successful result of the API call', async() => {
    const apiUrl = "https://rickandmortyapi./api/character";
    await axios.get(apiUrl)
      .then(r => {
        expect(r.data).toBeDefined();
        expect(r.data.results.length).toBeGreaterThan(0);
        expect(r.status).toBeGreaterThanOrEqual(200);
        expect(r.status).toBeLessThan(300);
      })
      .catch(e => {
        fail(`Expected successful response`);
      });
  });

  test('Get failure result of the API call', async() => {
    const apiUrl = "https://rickandmortyapi./api/character-bad";
    await axios.get(apiUrl)
      .then(r => {
        fail(`Expected failure response`);
      })
      .catch(e => {
        if (e.response) {
          expect(e.response.status).toBeGreaterThanOrEqual(400);
          expect(e.response.status).toBeLessThan(500);
        } else {
          throw e;
        }
      });
  });
});

发布评论

评论列表(0)

  1. 暂无评论