Babel.js Jest在test.tsx文件中遇到意外的令牌[React-native]

yx2lnoni  于 2023-09-28  发布在  Babel
关注(0)|答案(1)|浏览(133)

我看到很多人都面临这个问题。我试过各种方法,但仍然无法解决。
在我的终端中出现以下错误:
FAILtests/App.test.tsx ●测试套件无法运行

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/mmt10901/Desktop/Code/B2B-Shopfront-Mobile/__tests__/App.test.tsx:37
    const component = renderer.create(<App_1.NavBody />);
                                      ^

SyntaxError: Unexpected token '<'

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1495:14)

以下是我的Jest配置:

module.exports = {
    preset: "ts-jest",
    testEnvironment: "node",
    setupFiles: ['./node_modules/react-native-gesture-handler/jestSetup.js'],

    setupFilesAfterEnv: ["<rootDir>/setupTests.js"],
    transformIgnorePatterns: [
        "node_modules/(?!(react-native|@react-native|@babel|ts-js)/)",
    ],
    moduleNameMapper: {
        '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
            '<rootDir>/assetsTransformer.js',
        '\\.(css|less)$': '<rootDir>/assetsTransformer.js',
    },
    // transform: {},
    transform: {
        "\\.tsx?$": "ts-jest",
        "\\.jsx?$": "babel-jest",
    },
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
    extensionsToTreatAsEsm: ['.ts', '.tsx'],
};

以下是我的Babel配置:

module.exports = (api) => {
  api.cache(false);

  return {
    presets: [
      "module:metro-react-native-babel-preset",
      "@babel/preset-typescript",
    ],
    plugins: [
      [
        "module:react-native-dotenv",
        {
          envName: "APP_ENV",
          moduleName: "@env",
          // paths: {
          //    "@env": ["node_modules/react-native-dotenv"],
          //    "react-native-dotenv": ["node_modules/react-native-dotenv"]
          // },
          path: ".env",
          safe: false,
          allowUndefined: true,
          verbose: false,
        },
      ],
    ],
  };
};

tsConfig.json

{
  "extends": "@tsconfig/react-native/tsconfig.json",
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "checkJs": true,
    "noEmit": true,
    "strict": false,
    "sourceMap": true,
    "noImplicitAny": false,
    "baseUrl": "./",
    "types": ["jest", "node"],
    "typeRoots": ["./src/types"],
    "paths": {
      "@/*": ["./*"]
    },
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": [
    ".eslintrc.js",
    "*.ts",
    "*.tsx",
    "src/**/*.ts",
    "src/**/*.tsx",
    "*/*.d.tsx",
    "*/**.d.ts",
    "./tsconfig.json",
    "./babel.config.js",
    "./jest.config.js"
  ],
  "exclude": ["node_modules"],
  "esModuleInterop": true
}

我正在使用“react-native”:“0.71.7”
请帮助我,因为这是一个基本的设置,我一直在破坏我的头脑,因为过去2天,试图找到一个解决方案。

8ljdwjyq

8ljdwjyq1#

为了解决Jest由于JSX标记的打开<上的意外令牌而失败的问题,通常需要设置正确的React运行时。为此,请尝试使用runtime: "automatic"配置@babel/preset-react(如果需要,请安装@babel/preset-react)。
也就是说,在一个普通的.babelrc文件中,你需要这样的东西:

{
  "presets": [
    ["@babel/preset-react", { runtime: "automatic" }]
  ]
}

相关问题