Start

2023-7-13 Less than 1 minute

To mock an http request, just call a mock method or http verb method(get,post,put,patch,delete).

import HttpRequestMock from 'http-request-mock';
const mocker = HttpRequestMock.setup();

mocker.mock({
  url: 'https://www.api.com/some-api' // or RegExp: /.*\/some-api$/
  body: 'some response data'
});

// or using http verb method:
mocker.get('https://www.api.com/some-api', 'some response data');

Then, you can write your business code as normal:

axios.get('https://www.api.com/some-api').then(res => {
  console.log(res.data); // some response data
});

Last update: July 13, 2023 22:53