如何模拟axios({method:“ GET”,url:“ / something”})请求?

时间:2020-06-29 14:46:19

标签: javascript reactjs react-native jestjs axios

// app/domain/index.js
export * from './negotiation';
export * from './negotiations';

// app/app.js
import { Negotiation, Negotiations } from './domain';

如何模拟这种类型的Axios请求?我正在使用Jest在本机反应项目中进行测试(没有EXPO)

1 个答案:

答案 0 :(得分:2)

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));
  });