为Jest测试设置Redux提供程序

xoshrz7s  于 11个月前  发布在  Jest
关注(0)|答案(2)|浏览(151)

我正在尝试用我的react应用程序为Jest测试设置一个redux提供程序。

import {ReactNode} from 'react'
import {Provider} from 'react-redux'
import {store} from "../redux/reducer/store";

type PropsWithOptionalChildren<P = unknown> = P & { children?: ReactNode };
type RootState = ReturnType<typeof store>    
interface ReduxProviderProps extends PropsWithChildren {
    store: RootState,
}

const ReduxProvider = ({children, store}: ReduxProviderProps) => <Provider store={store}>{children}</Provider>

export default ReduxProvider

字符串
然后我就像这样

test("...", () => {
  const store = configureStore();
  const wrapper = ({ children }) => (
    <ReduxProvider reduxStore={store}>{children}</ReduxProvider>
  );
  ...
});


但是,编译器会对下面的代码行发出抱怨

type RootState = ReturnType<typeof store>


类型[...]不满足约束(...args:any)=> any类型[...]未提供签名的匹配项(...args:any):any
这是store变量

export const store = configureStore({
    reducer: persistedReducer,
    devTools: process.env.NODE_ENV !== 'production',
    middleware: getDefaultMiddleware =>
        getDefaultMiddleware({
            serializableCheck: false
        }).concat(apiSlice.middleware),
});


和附加信息

export const rootReducer = combineReducers({
    a: aReducer,
    b: bReducer,
    [apiSlice.reducerPath]: apiSlice.reducer,
})

const config = getPersistConfig({
    key: 'root',
    storage,
    blacklist: ['a'],
    rootReducer
})

const persistedReducer = persistReducer(config, rootReducer)


如何解决编译器错误?

ffscu2ro

ffscu2ro1#

试试这个:

type RootState = ReturnType<typeof store.getState>;

字符串

tuwxkamq

tuwxkamq2#

将RootState类型更改为如下所示,并查看它是否解决
第一个月

相关问题