ReactNative Jest快照测试失败

时间:2018-07-09 10:42:25

标签: reactjs typescript react-native jestjs

我想将TypeScript添加到我的react-native项目中。我创建了this article,并逐步进行了开发。但是当我运行yarn test时出现此错误,我也不知道为什么:

FAIL  Components/__tests__/Hello.tsx
● Test suite failed to run

Plugin 0 specified in ".../node_modules/babel-preset-react-native/index.js" provided an invalid property of "default"(While processing preset: ".../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)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.865s
Ran all test suites.
error Command failed with exit code 1.

这是我的package.json:

{
  "name": "TypeScriptLab",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.4.1",
    "react-native": "0.56.0"
  },
  "devDependencies": {
    "@types/jest": "^23.1.4",
    "@types/react": "^16.4.6",
    "@types/react-native": "^0.55.28",
    "@types/react-test-renderer": "^16.0.1",
    "babel-jest": "23.2.0",
    "babel-preset-react-native": "^5",
    "jest": "23.3.0",
    "react-addons-test-utils": "^15.6.2",
    "react-native-typescript-transformer": "^1.2.10",
    "react-test-renderer": "16.4.1",
    "ts-jest": "^23.0.0",
    "typescript": "^2.9.2"
  },
  "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "transform": {
      "^.+\\.(js)$": "<rootDir>/node_modules/babel-jest",
      "\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
    },
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
    "testPathIgnorePatterns": [
      "\\.snap$",
      "<rootDir>/node_modules/"
    ],
    "cacheDirectory": ".jest/cache"
  }
}

这是我的Hello.tsx

import React from 'react'
import renderer from 'react-test-renderer'

import { Hello } from '../Hello'

it('renders correctly with defaults', () => {
  const button = renderer
    .create(<Hello name={'World'} enthusiasmLevel={1} />)
    .toJSON()
  expect(button).toMatchSnapshot
})

我在Internet上进行了搜索,但是找不到解决此问题的任何解决方案。 谁能帮我吗?

1 个答案:

答案 0 :(得分:4)

最后,我从package.json文件中删除了这一行
"babel-preset-react-native": "^5"

然后再次安装此软件包,以便现在可以正常使用。
"babel-preset-react-native": "^4.0.0",