Jest遇到了一个带有react-native的意外令牌 [英] Jest encountered an unexpected token with react-native

查看:72
本文介绍了Jest遇到了一个带有react-native的意外令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图用Jest和TypeScript在我的React Native应用上编写测试.使用旧的babel版本时,一切正常,但是由于某些项目问题,我们不得不将babel升级到7.0.0.在那之后,我无法使其工作.感谢您的帮助

So I'm trying to write tests on my React Native app with Jest and TypeScript. While using old babel version everything worked fine, but because of some project problems we had to upgrade babel to 7.0.0. After that I couldn't make it work. Any help is appreciated

Jest遇到意外令牌 这通常意味着您正在尝试导入Jest无法解析的文件,例如这不是普通的JavaScript. 默认情况下,如果Jest看到Babel配置,它将使用该配置来转换文件,而忽略"node_modules".

Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript. By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

/home/levan/work/vabaco-dhp-frontend/packages/dhp-mobile-app/node_modules/react-native-calendar-events/index.ios.js:3 从'react-native'导入{NativeModules};

/home/levan/work/vabaco-dhp-frontend/packages/dhp-mobile-app/node_modules/react-native-calendar-events/index.ios.js:3 import { NativeModules } from 'react-native';

SyntaxError:意外的令牌导入

SyntaxError: Unexpected token import

package.json:

package.json:

"dependencies": {
    "react": "16.4.1",
    "react-native": "0.56.0",
    "react-native-calendar-events": "^1.6.1",
    "react-redux": "^5.0.7",
    "react-router": "^4.3.1",
    "react-router-native": "^4.3.0",
    "react-router-redux": "5.0.0-alpha.9",
    "redux": "^4.0.0",
    "redux-api-middleware": "^2.3.0",
    "redux-form": "^7.4.2",
    "redux-thunk": "^2.3.0",
    "urijs": "^1.19.1"
  },
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@types/history": "^4.7.0",
    "@types/jest": "^23.3.1",
    "@types/react": "^16.4.13",
    "@types/react-native": "^0.56.17",
    "@types/react-router": "^4.0.30",
    "@types/react-router-native": "^4.2.3",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^23.4.2",
    "babel-loader": "^8.0.2",
    "babel-preset-react-native": "^5.0.0",
    "jest": "^23.5.0",
    "react-native-typescript-transformer": "^1.2.10",
    "react-test-renderer": "^16.4.2",
    "ts-jest": "^23.1.4",
    "typescript": "^3.0.3"
},


"jest": {
    "preset": "react-native",
    "transform": {
      "^.+\\.ts?$": "ts-jest",
      "^.+\\.tsx?$": "ts-jest",
      "^.+\\.js$": "babel-jest"
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)?$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "moduleNameMapper": {
      "^.+\\.(css|less|scss)$": "identity-obj-proxy"
    },
    "transformIgnorePatterns": [
      "/node_modules/(?!(react-native|my-project|react-native-button|react-native-calendar-events)/)"
    ]
  }

babelrc:

{
  "presets": ["react-native", ["@babel/preset-env", {"modules": "commonjs"}]],
  "env": {
    "test": {
      "presets": ["react-native", ["@babel/preset-env"]]
    }
  }
}

推荐答案

找到了解决方案.在转换中,您应该使用react-native/jest/preprocessor.js而不是babel-jest.

Found the solution. In transform you should use react-native/jest/preprocessor.js instead of babel-jest.

"jest": {
    "preset": "react-native",
    "moduleDirectories": [
      "node_modules",
      "src"
    ],
    "transform": {
      "^.+\\.ts?$": "ts-jest",
      "^.+\\.tsx?$": "ts-jest",
      "^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js" <--- over here
    },
    "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)?$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ],
    "moduleNameMapper": {
      "^.+\\.(css|less|scss)$": "identity-obj-proxy"
    },
    "transformIgnorePatterns": []
}

这篇关于Jest遇到了一个带有react-native的意外令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆