使用测试库测试Redux时出现此错误:警告:React创建元素:类型无效:但得到:未定义

tjjdgumg  于 2022-11-12  发布在  React
关注(0)|答案(1)|浏览(92)

我正在尝试使用测试库测试React Native App redux存储,我按照Redux文档创建了一个 Package 提供程序,以便将存储传递给要测试的组件,在本例中是我的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)

这是我从redux文档中复制的“test-utils.tsx”:

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();
});
});
});

我希望有些人能遇到一些建议,以解决这个问题,我有几天试图解决它。谢谢。

nwlls2ji

nwlls2ji1#

我找到了解决办法。我被嘲笑复活了两次,我只是删除了其中一个,现在工作正常。所以要小心你在嘲笑什么和如何嘲笑。

相关问题