You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native项目Jest测试导入react-redux Provider报错求助

React Native Jest测试中react-redux导入失败报错SyntaxError

错误详情

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)

测试工具代码(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"; // 此处导入失败

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

// 重新导出所有内容
export * from "@testing-library/react-native";

// 覆盖render方法
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
  },

// ...
}

问题原因

Jest默认忽略node_modules目录下的文件转译,而react-redux新版本输出的是ES模块代码(使用import/export语法),当前transformIgnorePatterns的白名单未包含react-redux,导致Jest无法转译该包的ES语法,直接执行时触发语法错误。

解决方案

  1. 修改Jest配置,添加react-redux到转译白名单
    更新package.json中的transformIgnorePatterns,把react-redux和其依赖@reduxjs/toolkit(若项目中使用)加入否定匹配列表:

    "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|react-redux|@reduxjs/toolkit)"
      ],
      "collectCoverage": true
    }
    

    这样Jest会对react-redux和@reduxjs/toolkit的代码进行转译,将ES模块语法转换为兼容的CommonJS语法。

  2. 辅助排查步骤

    • 执行jest --clearCache清除Jest缓存,确保新配置生效
    • 若项目有单独的babel.config.js,确认配置包含@babel/preset-env、@babel/preset-react和@babel/preset-typescript(针对TypeScript项目),保证转译能力正常

内容的提问来源于stack exchange,提问作者reactive

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 21:05:03