Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Unit testing 无法模拟我的react action thunk玩笑中使用的模块函数_Unit Testing_Reactjs_Jestjs - Fatal编程技术网

Unit testing 无法模拟我的react action thunk玩笑中使用的模块函数

Unit testing 无法模拟我的react action thunk玩笑中使用的模块函数,unit-testing,reactjs,jestjs,Unit Testing,Reactjs,Jestjs,我目前正在尝试测试我的thunk操作getUserFeatureNames,以查看它是否使用jest调用成功操作GetUserFeatureNamesSuccess。getUserFeatureNames thunk操作当前驻留在loginActions.js文件中,该文件是我试图模拟的导入HomeQueries。到目前为止,我在运行jest测试时遇到以下错误 TypeError:\u homeQueries2.default.getFeatureNames不是函数 如何模拟homeQuerie

我目前正在尝试测试我的thunk操作getUserFeatureNames,以查看它是否使用jest调用成功操作GetUserFeatureNamesSuccess。getUserFeatureNames thunk操作当前驻留在loginActions.js文件中,该文件是我试图模拟的导入HomeQueries。到目前为止,我在运行jest测试时遇到以下错误

TypeError:\u homeQueries2.default.getFeatureNames不是函数

如何模拟homeQueries.getFeatureNames

function createStore(state = {}, expectActions = {}){
  const mockStore = configureMockStore([thunk]);
  return mockStore(state, expectActions);
}

describe("home_async_tests", () => {

test("getUserFeatureNamesSuccess action is called if request was success", (done) => {

jest.mock('../../../graphQL/homeQueries', () => {
  return jest.fn(() => {
     {
      getFeatureNames: () =>{
          return new Promise((resolve, reject) => {
            let array = [{iconFile: 'Personalization.png', description: 'Personalization'},{iconFile: 'Home.png', description: 'Home'}];
            resolve(array);
          });
      };
    }
  });

});
jest.dontMock('../../../app/redux/actions/homeActions');
let homeActions = require('../../../app/redux/actions/homeActions');
const expectedAction = {type: types.userFeatureNamesSuccess, payLoad: {isError: false, data: '' }};
const store = createStore();
store.dispatch(homeActions.getUserFeatureNames({token:"fdis4554" })).then(() => {
  const actions = store.getActions();
  expect(actions[0].type).toEqual(expectedAction.type);
  expect(actions[0].payLoad.isError).toEqual(expectedAction.payLoad.isError);
  done();
});

});

我假设模块只返回一个对象,而不是返回一个对象的函数,因此您的模拟应该如下所示:

jest.mock('../../../graphQL/homeQueries', () = > ({
    getFeatureNames: () = > {
      return new Promise((resolve, reject) = > {
        let array = [{
          iconFile: 'Personalization.png',
          description: 'Personalization'
        }, {
          iconFile: 'Home.png',
          description: 'Home'
        }];
        resolve(array);
      });
    };
  }
});