当前位置:  开发笔记 > 编程语言 > 正文

React Native - Jest:自更新到0.56后损坏.怎么解决?

如何解决《ReactNative-Jest:自更新到0.56后损坏.怎么解决?》经验,为你挑选了2个好方法。

自从升级到新的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 ()
      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 ()
      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',它会给出上面的错误.



1> Kanekotic..:

@J.Hesters解决方案几乎是正确的.但是没有必要安装babel,只需添加转换即可解决问题.

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

我之前的回复有一些副作用,直到我尝试重建整个反应本机应用程序后才看到.所以请不要使用它.

以前的答案

实际上与react-native 0.56无关.是由babel预设5引起的,只是还原这个包就行了.

yarn add babel-preset-react-native@4.0.0



2> J. Hesters..:

这是我修复它的方法.

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

对你package.json而言npm install.这为我修好了.

此外,对于酶,你将不得不添加

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

你的jest配置package.json.

如果您想查看更全面的修复,请查看此github问题.

推荐阅读
夏晶阳--艺术
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有