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