Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用jest模拟axios不会返回值_Javascript_Reactjs_React Native_Testing_Jestjs - Fatal编程技术网

Javascript 用jest模拟axios不会返回值

Javascript 用jest模拟axios不会返回值,javascript,reactjs,react-native,testing,jestjs,Javascript,Reactjs,React Native,Testing,Jestjs,我正在对刷新用户jwt令牌的函数进行单元测试。在该功能中,必须发出post请求。我正在模拟axios,在mock文件夹中指定数据时可以返回正确的数据。但是,我希望能够在测试本身中指定要解析的数据,因为我希望使用不同类型的返回数据进行尝试 //__mock__/axios.js module.exports = { get: jest.fn(), post: jest.fn(), create: () => { return { get: () => {

我正在对刷新用户jwt令牌的函数进行单元测试。在该功能中,必须发出post请求。我正在模拟axios,在mock文件夹中指定数据时可以返回正确的数据。但是,我希望能够在测试本身中指定要解析的数据,因为我希望使用不同类型的返回数据进行尝试

//__mock__/axios.js
module.exports = {
  get: jest.fn(),
  post: jest.fn(),
  create: () => {
    return {
      get: () => {
        return {};
      },
      post: () => {
        return {};
      }
    };
  }
};

  //unit.test.tsx
      test.only('succesfully sets and gets auth token', async () => {
        const SecureStore = require('expo-secure-store');
        const data = {
          accessToken: 'token',
          refreshTOken: 'refresh'
        };
        let mockSetItemAsync;
        let mockGetItemAsync;
        mockSetItemAsync = jest.fn();
        mockGetItemAsync = jest.fn();
        mockGetItemAsync = jest
          .spyOn(SecureStore, 'getItemAsync')
          .mockImplementation(() => Promise.resolve({ token: 'token' }));
        mockSetItemAsync = jest
          .spyOn(SecureStore, 'setItemAsync')
          .mockImplementation(() => Promise.resolve({ success: true }));
        SecureStore.setItemAsync = mockSetItemAsync;
        SecureStore.getItemAsync = mockGetItemAsync;
    
        const mockAxios = jest
          .spyOn(mockedAxios, 'post')
          .mockImplementationOnce(() => Promise.resolve(data));
    
        await OAuth2.refreshAuth();
        expect(mockGetItemAsync).toHaveBeenCalled();
        expect(mockSetItemAsync).toHaveBeenCalled();
        expect(mockAxios).toHaveBeenCalledWith({ token: 'token' });
      });
但是,如果我将mock文件夹中的数据指定为:

module.exports = {
  get: jest.fn(),
  post: jest.fn(() =>
    Promise.resolve({ data: { accessJWT: 'token', refreshJWT: 'refresh' } })
  ),
  create: () => {
    return {
      get: () => {
        return {};
      },
      post: () => {
        return {};
      }
    };
  }
我得到了正确的返回值。
如何在测试本身中重写返回值?

我处理它的方法就是用预期的返回值显式地重新声明mock。为了清楚起见,除了一般的mock之外,我还将在测试文件的顶部导入axios,并编写类似于:axios.post=jest.fn(…)