javascript - 与 React Native 开 Jest 得到问题

标签 javascript reactjs react-native jestjs babel-jest

我在尝试运行测试用例代码时遇到错误。我正在使用 react native with jest 。在升级 0.40 之前一切正常。现在是 0.42,我所有的测试用例都停止工作并在错误之后出现错误。

({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React, { Component, Children, PropTypes } from 'react';
                                                                                             ^^^^^^
    SyntaxError: Unexpected token import

      at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
      at Object.<anonymous> (node_modules/react-native-root-siblings/lib/AppRegistryInjection.js:3:22)
      at Object.<anonymous> (node_modules/react-native-root-siblings/lib/SiblingsManager.js:3:27)

这是我的.babelrc代码

{
    "presets": [
        "react-native"
    ],
    "plugins": [
        "transform-decorators-legacy"
    ]
}

我没有得到什么问题。

最佳答案

我遇到了与 0.42 完全相同的问题。我一直在敲脑袋,直到找到一个解决方案。

你需要在package.json中写ignores。我的例子:

"jest": {
        "preset": "react-native",
        "setupFiles": [
            "<rootDir>/src/config/jest.js"
        ],
        "transformIgnorePatterns": [
            "<rootDir>/(node_modules)/(?!react-native|react-navigation|bugsnag-react-native)"
        ],
        "transform": {
            "^.+\\.js$": "<rootDir>/node_modules/babel-jest"
        }

我的 /config/jest.js 看起来像:

jest.mock('Linking', () => ({
  addEventListener: jest.fn(),
  removeEventListener: jest.fn(),
  openURL: jest.fn(),
  canOpenURL: jest.fn(),
  getInitialURL: jest.fn().mockImplementation(() => new Promise((resolve) => resolve()))
}));

jest.mock('mobx-react/native', () => require('mobx-react/custom'));
jest.mock('react-native-mixpanel', () => ({
  sharedInstanceWithToken: jest.fn(),
  trackWithProperties: jest.fn()
}));
jest.mock('bugsnag-react-native', () => ({
  Client: jest.fn(),
  Configuration: jest.fn()
}));

我不保证这会直接解决您的所有问题。但是,我们的想法是忽略所有“作恶者”(在您的情况下为 react-native-root-siblings),从而避免此类错误消息。

关于javascript - 与 React Native 开 Jest 得到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43137058/

相关文章:

javascript - Aurelia 功能 globalResources (jspm)

reactjs - 切换到在现有项目上创建 React 应用程序?

javascript - 来自 react-native 和 react-native-gesture-handler 的 Touchable 之间的区别

react-native - 在 react-native 的 ImageView 中从相机获取最近点击的图像

react-native - 异步存储 {"40":0 ,"_65":0 ,"_55":null ,"_72":null} in React native

javascript - 为什么++[[]][0]返回1而不是报错?

javascript - 在 AngularJS 中使用 underscore.js 合并两个数组

javascript - 简单的 JavaScript 不执行?

reactjs - Material ui usestyles 已弃用 - 我需要更改代码吗

javascript - React HOC 回调不适用于默认 Prop