正在测试Redux,测试库获取此错误:警告:React.createElement:类型无效:但获取:未定义



我正试图使用测试库测试我的React Native App redux存储,我遵循redux文档创建了一个Wrapper提供程序,以便将存储传递给要测试的组件,在本例中是我的LoginScreen组件。但我得到了以下错误:

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. Check the render method of `LoginScreen`.
at LoginScreen (/Users/macbookpro/Proyectos/Personales/antifappi/src/screens/notauthenticated/LoginScreen.tsx:41:22)
at Provider (/Users/macbookpro/Proyectos/Personales/app/node_modules/react-redux/lib/components/Provider.js:19:3)
at Wrapper (/Users/macbookpro/Proyectos/Personales/app/src/utils/test-utils.tsx:29:22)

这是我的">测试utils.tsx";我从redux文档中复制的:

export const renderWithProviders = (
ui: React.ReactElement,
{
preloadedState = {},
// Automatically create a store instance if no store was passed in
store = setupStore(preloadedState),
...renderOptions
}: ExtendedRenderOptions = {}
) => {
const Wrapper = ({ children }: PropsWithChildren<{}>): JSX.Element => {
return (
<Provider store={ store }>
{ children }
</Provider>
)
}
return rtlRender(ui, { wrapper: Wrapper, ...renderOptions }) 

}

这是我对loginscreen:的测试

describe("Login Screen", () => {
describe("layout", () => {
it("render correctly", async () => {
component = renderWithProviders(<LoginScreen />, { preloadedState: {
auth: {
status: 'checking',
loadingAuth: false,
user: null,
message: '',
token: null,
goSettingUp: false,
}
}, store: store});
console.log(component);
expect(component).toBeDefined();
});
});
});

我希望有人能给我一些建议来解决我花了几天时间试图解决的问题。谢谢。

我找到了解决方案。我被嘲笑了两次,我刚刚删除了其中一个,现在一切都很好。所以要小心你在嘲笑什么以及如何嘲笑。

您可能已经在测试文件中编写了以下内容

jest.mock('react-redux', ...

只需删除该部分,或者如果您想模拟一些redux属性,请使用以下代码示例(typesript(

jest.mock('react-redux', () => {
const originalModule = jest.requireActual<typeof import('react-redux')>('react-redux');
return {
...originalModule,
// you code here
};
});

最新更新