reactjs - React Native - Jest : Broken since update to 0. 56. 如何修复它?

标签 reactjs react-native jestjs babeljs babel-jest

自从升级到新的 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”,它给出了上面的错误。

最佳答案

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

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

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

上一个答案

实际上与react-native 0.56无关。是由 babel 预设 5 引起的,只需恢复此包即可解决问题。

yarn 添加 babel-preset-react-native@4.0.0

关于reactjs - React Native - Jest : Broken since update to 0. 56. 如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51197405/

相关文章:

javascript - React 无法从文档/窗口按键访问状态

javascript - 如何在 VS Code IDE 中查看文件的历史记录?

reactjs - React/Redux 架构中 API 调用应该放在哪里?

javascript - 测试 gRPC 函数

reactjs - 用 enzyme : Unexpected token in mount() 测试

javascript - 传递表单数据快速 react redux

javascript - 在 react-native-tab-view 中创建路由时出现不变冲突

javascript - onesignal 与 react 导航和 redux : i am not able to navigate to screen when notification is opened

javascript - React Native 文本输入掩码

javascript - Jest 在 react-native 中遇到了意外的标记