Jest 自定义渲染导入 React-Redux 提供程序错误

问题描述 投票:0回答:1

我正在尝试对react-native项目进行一些测试,但是jest遇到了这个错误,我不知道为什么:

 FAIL  __tests__/(tabs)/weather.test.tsx
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /Users/j/Downloads/tdd-expo/node_modules/react-redux/dist/react-redux.legacy-esm.js:34
    import * as React2 from "react";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      3 | import { render } from "@testing-library/react-native";
      4 | import React, { PropsWithChildren } from "react";
    > 5 | import { Provider } from "react-redux";
        | ^
      6 |
      7 | const AllTheProviders = ({ children }: PropsWithChildren) => {
      8 |   return (

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
      at Object.require (test-utils.tsx:5:1)
      at Object.require (/Users/j/Downloads/tdd-expo/__tests__/(tabs)../../../../weather.test.tsx:2:1)

这似乎是失败的:

    Details:

    /Users/j/Downloads/tdd-expo/node_modules/react-redux/dist/react-redux.legacy-esm.js:34
    import * as React2 from "react";
    ^^^^^^

因此,无法将其导入到

test-utils.tsx
文件中:

// test-utils.tsx
import { DarkTheme, ThemeProvider } from "@react-navigation/native";
import "@testing-library/jest-native/extend-expect";
import { render } from "@testing-library/react-native";
import React, { PropsWithChildren } from "react";
import { Provider } from "react-redux"; // Is failing to import here

const AllTheProviders = ({ children }: PropsWithChildren) => {
  return (
    <Provider store={{} as any}>
      <ThemeProvider value={DarkTheme}>{children}</ThemeProvider>
    </Provider>
  );
};

const customRender = (ui: React.ReactElement, options: any) =>
  render(ui, { wrapper: AllTheProviders, ...options });

// re-export everything
export * from "@testing-library/react-native";

// override render method
export { customRender as render };

这是 jest 的配置:

// package.json
{
// ...
  "jest": {
    "preset": "jest-expo",
    "transformIgnorePatterns": [
      "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@unimodules/.*|unimodules|sentry-expo|native-base|react-native-svg)"
    ],
    "collectCoverage": true
  },

// ...
}

我遵循了这个指南: https://docs.expo.dev/develop/unit-testing/

并尝试了这个自定义渲染设置: https://testing-library.com/docs/react-testing-library/setup/#custom-render

我期望 jest 能够成功运行,但实际上导致了“导入”错误。

知道发生了什么吗?预先感谢😊

react-native unit-testing react-redux jestjs expo
1个回答
0
投票

我也有这个问题,有什么解决办法吗?

© www.soinside.com 2019 - 2024. All rights reserved.