使用 Jest/redux-mock-store 模拟 Redux 存储的问题



我一直试图模拟依赖模块中的 redux 存储,但没有成功,如下所示:

//configStore.js
const store = createStore(
combineReducers(reducers),
undefined,
composeEnhancers(
autoRehydrate(),
applyMiddleware(...middlewares)
// other store enhancers if any
)
);

export default store;

此存储用于如下所示的文件中:

//module
import store from '../config/configStore';
export function reSeat() {
let seat = store.getState().travelers[0].accommodations[0];
//some logic
store.dispatch(API_CALL);
store.dispatch(RE_SEAT);
}

我的测试:

import store from "../../../app/config/configStore";
import * as module from '../../../app/actions/module';
import configureStore from 'redux-mock-store'

jest.mock('../../../app/config/configStore', () => {
return {
__esModule: true,
default: configureStore([])({})
};
});

describe('module action', () => {
console.log(store.getState()); 
console.log(module.reseat());
//it will print the 'real' store initial state based on the reducers on both cases :-(
...
...
...

我尝试按照此链接上的说明进行操作:https://remarkablemark.org/blog/2018/06/28/jest-mock-default-named-export/

看起来我的 mock.jest 根本没有被调用。有人对此有任何想法吗?

我发现了这个问题。看起来将转换器添加到开玩笑的配置(package.json(并没有让.mock工作,不知道为什么。

我刚刚卸下了.js变压器,它工作了:

"transform": {
"^.+\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js", //removed this line
"^.+\.jsx?$": "babel-jest"
}

最新更新