【问题标题】:Babel not transpiling Expo module with Mocha testBabel 没有使用 Mocha 测试转译 Expo 模块
【发布时间】:2018-02-01 20:03:48
【问题描述】:

当使用 Mocha 运行单元测试时,我们会收到以下令牌异常,它指向 Babel 未转译。

node_modules/expo/src/Expo.js:2
import './environment/validate';
^^^^^^

SyntaxError: Unexpected token import

mocha 测试指向一个从 expo 导入 Constants 的文件

import { Constants } from 'expo'

const config = Constants.manifest.extra

这就是 mocha opts 和 babel 的配置方式

test/mocha.opts

--recursive
--require babel-register

.babelrc

{
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    }
  }
}

依赖关系

{
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-eslint": "^7.2.3",
    "chai": "^4.1.2",
    "eslint": "^3.19.0",
    "eslint-config-airbnb": "^15.0.1",
    "eslint-plugin-import": "^2.3.0",
    "eslint-plugin-jsx-a11y": "^5.0.3",
    "eslint-plugin-react": "^7.0.1",
    "jest-expo": "^24.0.0",
    "mocha": "^5.0.0",
    "react-native-scripts": "1.0.0",
    "react-test-renderer": "16.0.0-alpha.6",
    "rimraf": "^2.6.2",
    "sinon": "^4.2.0"
  },
  "dependencies": {
    "axios": "^0.16.2",
    "dot-object": "^1.7.0",
    "expo": "^24.0.0",
    "google-libphonenumber": "^3.0.5",
    "jwt-decode": "^2.2.0",
    "lodash": "^4.17.4",
    "moment": "^2.19.3",
    "prop-types": "^15.5.10",
    "react": "16.0.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-24.0.0.tar.gz",
    "react-native-google-places-autocomplete": "^1.3.6",
    "react-native-modal": "^4.1.1",
    "react-native-modal-datetime-picker": "^4.12.0",
    "react-native-swipeable": "^0.6.0",
    "react-native-vector-icons": "^4.2.0",
    "react-navigation": "^1.0.0-beta.21",
    "react-redux": "^5.0.5",
    "redux": "^3.6.0",
    "redux-devtools": "^3.4.0",
    "redux-devtools-dock-monitor": "^1.1.2",
    "redux-devtools-log-monitor": "^1.3.0",
    "redux-logger": "^3.0.6",
    "redux-saga": "^0.15.3",
    "redux-thunk": "^2.2.0",
    "styled-components": "^2.0.1"
  }
}

我们正在运行

yarn mocha

【问题讨论】:

  • 嗨@ptimson,你能运行mocha test

标签: javascript react-native mocha.js babeljs expo


【解决方案1】:

我刚刚遇到了类似的错误 - 我在单个文件中使用 Expo.AuthSession 后,我的许多套件都会中断。

我通过添加解决了我的问题

jest.mock('expo', () => ({ AuthSession: { getRedirectUrl: jest.fn(), }, }));

到我的全局模拟文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多