React Native中的Jest测试失败

时间:2019-01-12 19:48:40

标签: react-native jestjs babel

我对React Native,移动应用程序开发和整个技术栈都是新手,所以假设我可能做错了最基本的事情。我正在尝试建立一个简单的React Native应用程序,但无法成功执行测试。

复制步骤:

  1. 运行react-native初始MyAwesomeApp
  2. 默认应用程序使用react-native run-android成功在模拟器中运行
  3. 创建__tests__文件夹
  4. 将基本测试myfirst.test.js添加到文件夹
const counter = (a) => a + 1;

describe('counter: Should increment the passed value', () => {
  expect(counter(1)).toBe(2);
});
  1. 使用npm test执行测试。

输出:

> trytest@0.0.1 test /home/xxxxx/MyAwesomeApp
> jest

 FAIL  __tests__/myfirst.test.js
  * Test suite failed to run

    Couldn't find preset "module:metro-react-native-babel-preset" relative to directory "/home/xxxxx/MyAwesomeApp"

      at node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
          at Array.map (<anonymous>)
      at OptionManager.resolvePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
      at OptionManager.mergePresets (node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
      at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
      at OptionManager.init (node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
      at File.initOptions (node_modules/babel-core/lib/transformation/file/index.js:212:65)
      at new File (node_modules/babel-core/lib/transformation/file/index.js:135:24)
      at Pipeline.transform (node_modules/babel-core/lib/transformation/pipeline.js:46:16)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.161s
Ran all test suites.
npm ERR! Test failed.  See above for more details.
  1. 经过大量谷歌搜索之后,我尝试解决的一件事似乎已解决了该特定错误:将.babelrc的内容从"presets": ["module:metro-react-native-babel-preset"]更改为"presets": ["react-native"]。我现在在执行测试时看到的错误是:
> trytest@0.0.1 test /home/xxxxx/MyAwesomeApp
> jest

 FAIL  __tests__/myfirst.test.js
  * Test suite failed to run

    Cannot find module 'AccessibilityInfo' (While processing preset: "/home/xxxxx/MyAwesomeApp/node_modules/react-native/Libraries/react-native/react-native-implementation.js")

      at Object.get AccessibilityInfo [as AccessibilityInfo] (node_modules/react-native/Libraries/react-native/react-native-implementation.js:22:12)
      at node_modules/lodash/_baseClone.js:163:23
      at arrayEach (node_modules/lodash/_arrayEach.js:15:9)
      at baseClone (node_modules/lodash/_baseClone.js:160:3)
      at cloneDeepWith (node_modules/lodash/cloneDeepWith.js:37:10)
      at OptionManager.mergeOptions (node_modules/babel-core/lib/transformation/file/options/option-manager.js:206:44)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.169s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

2 个答案:

答案 0 :(得分:1)

对于我来说,让它适用于本机版本0.57.8,我做到了: 将.babelrc更改为:

{ 
  "presets": ["react-native"]
}

似乎您需要安装babel-preset-react-native。所以: yarn add babel-preset-react-nativenpm install babel-preset-react-native

不幸的是,当我们进行此更改时,我们开始遇到构建问题。因此,我想有一种更好的方法来解决问题。

然后我查看了thread,发现它对我有用(无需更改.babelrc文件)。

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

答案 1 :(得分:0)

您应在Package.json中检查Jest版本,并在需要时进行更新