【问题标题】:How would one mock an axios({method: "GET", url:"/something"}) request?如何模拟 axios({method: "GET", url:"/something"}) 请求?
【发布时间】:2020-10-19 18:23:55
【问题描述】:
axios({method: "GET", url:"/something"})

如何模拟这种类型的 Axios 请求?我正在使用 jest 在 react-native 项目中进行测试(没有博览会)

【问题讨论】:

标签: javascript reactjs react-native jestjs axios


【解决方案1】:
import axios from 'axios';
 
jest.mock('axios');
 
describe('fetchData', () => {
  it('fetches successfully data from an API', async () => {
    const data = {
      data: {
      },
    };
 
    axios.get.mockImplementationOnce(() => Promise.resolve(data));
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-18
    • 2021-08-01
    • 2019-07-27
    • 2019-05-06
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    相关资源
    最近更新 更多