React Native - Jest:自从更新到 0.56 后就损坏了。如何解决?

React Native - Jest: Broken since update to 0.56. How to fix it?

自从升级到新的 React Native 版本后,我的测试就失败了。

环境

这是我的环境:

React Native Environment Info:
System:
  OS: macOS High Sierra 10.13.4
  CPU: x64 Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz
  Memory: 486.81 MB / 16.00 GB

  Shell: 3.2.57 - /bin/bash
Binaries:
  Node: 9.4.0 - /usr/local/bin/node
  npm: 6.1.0 - /usr/local/bin/npm
  Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
  iOS SDK:
    Platforms: iOS 11.4, macOS 10.13, tvOS 11.4, watchOS 4.3
IDEs:
  Android Studio: 3.1 AI-173.4697961
  Xcode: 9.4.1/9F2000 - /usr/bin/xcodebuild
npmPackages:
  react: 16.4.1 => 16.4.1
  react-native: 0.56.0 => 0.56.0
npmGlobalPackages:
  create-react-native-app: 1.0.0
  react-native-cli: 2.0.1
  react-native-git-upgrade: 0.2.7

描述

在尝试测试我新创建的动作创建器时,npm test 抛出以下错误:

jest

 FAIL  app/actions/logout/logout.test.js
  ● Test suite failed to run


    Plugin 0 specified in "/Users/jan/Startup/react-native/ordersome/node_modules/babel-preset-react-native/index.js" provided an invalid
property of "default" (While processing preset: "/Users/jan/Startup/react-native/ordersome/node_modules/babel-preset-react-native/index.js
")

      at Plugin.init (node_modules/babel-core/lib/transformation/plugin.js:131:13)
      at Function.normalisePlugin (node_modules/babel-core/lib/transformation/file/options/option-manager.js:152:12)
      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:184:30
          at Array.map (<anonymous>)
      at Function.normalisePlugins (node_modules/babel-core/lib/transformation/file/options/option-manager.js:158:20)
      at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:234:36)
      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:265:14
      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:323:22
          at Array.map (<anonymous>)
      at OptionManager.resolvePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)

我用谷歌搜索了很多,但找不到解决办法。好像跟babel有点关系

可重现的演示

它很容易重现(目前)。我尝试使用 react-native init 开始一个新项目。然后我只是创建了一个随机 .test.js 文件并在其中编写了文档的入门测试:

function sum(a, b) {
  return a + b;
}

test("adds 1 + 2 to equal 3", () => {
  expect(sum(1, 2)).toBe(3);
});

接下来我运行 'npm test' 出现上面的错误。

0.56.0 有问题。

The only solution is running it with v0.55.4

通过以下方式安装 v0.55.4:npm i --save react-native@0.55.4

或者当 运行通过以下方式 运行 应用程序 运行 时:react-native run-android --version 0.55.4

这对我有帮助,希望对你有帮助

这是我修复它的方法。

添加

"@babel/core": "^7.0.0-beta.47",
"babel-core": "^7.0.0-beta.47",

你的 package.json 和 运行 npm install。这为我修好了。

此外,对于酶,您还必须添加

   "transform": {
      "^.+\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
    }

在你的 package.json.

中开玩笑配置

如果您想查看更彻底的修复,请查看 this github issue.

@J。海丝特的解决方案几乎是正确的。但不需要安装 babel,只需添加 transform 即可解决问题。

{
  ...
  "jest": {
    "preset": "react-native",
    "transform": {
      "^.+\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
    }
  }
  ...
}

我之前的回复有一些副作用,直到尝试重建整个 react-native 应用程序后我才看到这些副作用。所以请不要使用它。

上一个答案

实际上与react-native 0.56无关。是由 babel presets 5 引起的,只需还原此包即可。

yarn add babel-preset-react-native@4.0.0

我也有同样的错误,我还补充了:

"transform": {
      "^.+\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
    }

但后来我在另一个模块中又犯了一个错误,要忽略它,您需要将此代码添加到您的 setup.jest.js 文件中:

jest.mock('react-native-router-flux', () => {
    return {
        Crashlytics: {
            crash: () => {},
        },
        Answers: {
            logCustom: () => {},
            logContentView: () => {},
        },
    }

所以在这段代码之后 jest 将忽略测试中 "react-native-router-flux" 模块中的错误。