简体   繁体   English

在 Expo 上运行时 Jest 无法识别 .jsx 文件

[英]Jest not recognizing .jsx files when running on Expo

I'm having trouble getting jest to run its test suite on .jsx files from my Expo program.我无法让 jest 在我的 Expo 程序中的 .jsx 文件上运行其测试套件。

My babel.config.js:我的 babel.config.js:

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
  };
};

my package.json:我的 package.json:

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject",
    "test": "jest --watchAll --no-cache"
  },
  "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,
    "collectCoverageFrom": [
      "**/*.{js,jsx}",
      "!**/coverage/**",
      "!**/node_modules/**",
      "!**/babel.config.js",
      "!**/jest.setup.js"
    ]
  },
  "dependencies": {
    "@babel/preset-env": "^7.16.0",
    "@mantine/hooks": "^3.1.7",
    "axios": "^0.24.0",
    "expo": "~43.0.0",
    "expo-status-bar": "~1.1.0",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.2",
    "react-native-material-textfield": "^0.16.1",
    "react-native-web": "0.17.1",
    "styled-components": "^5.3.3"
  },
  "devDependencies": {
    "@babel/core": "^7.16.0",
    "@testing-library/react": "^12.1.2",
    "babel-jest": "^27.3.1",
    "babel-preset-react-native": "^4.0.1",
    "jest": "^27.3.1",
    "jest-expo": "^43.0.1",
    "prettier": "2.4.1",
    "react-test-renderer": "^17.0.2"
  },
  "private": true
}

I've also tried adding @babel/preset-react to presets , but the error message changes to the following:我也尝试将@babel/preset-reactpresets ,但错误消息更改为以下内容:

    [BABEL]: Cannot find module '@babel/core'

I do not have a webpack.config.js or a jest.config.js.我没有 webpack.config.js 或 jest.config.js。

If anyone has any ideas on how to fix this, I would be grateful.如果有人对如何解决此问题有任何想法,我将不胜感激。

Thanks!谢谢!

Edit: I've added '@babel/preset-react' to my babel.config.js file, and now I'm getting an error from jest that says 'add @babel/preset-react' to the 'presets' section of your Babel config to enable transformation.编辑:我已将“@babel/preset-react”添加到我的 babel.config.js 文件中,现在我收到了来自 jest 的错误消息,提示“将 @babel/preset-react”添加到“预设”部分您的 Babel 配置以启用转换。 My updated babel.config.js:我更新的 babel.config.js:

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo', '@babel/preset-react'],
    plugins: ['@babel/plugin-syntax-jsx']
  };
};

So I reinstalled all my dependencies and it seems to be working.... unsure what the issue was but it seems likely that I either deleted or broke a dependency file.所以我重新安装了我的所有依赖项,它似乎正在工作......不确定问题是什么,但似乎我删除或破坏了依赖项文件。

-OP -OP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM