1

我正在使用以下 package.json (根据http://facebook.github.io/jest/docs/tutorial-react-native.html#content):

{
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest",
  },
  "dependencies": {
    "react": "^15.3.1",
    "react-native": "0.31.0",
  },
  "devDependencies": {
    "babel-jest": "^14.1.0",
    "babel-preset-react-native": "^1.9.0",
    "jest": "^14.1.0",
    "jest-cli": "^13.1.0",
    "jest-react-native": "^14.1.3",
    "react-test-renderer": "^15.3.1"
  },
  "jest": {
    "globals": {
      "__DEV__": true,
      "__RCTProfileIsProfiling": false
    },
    "preset": "jest-react-native"
  }
}

但我得到了错误:

Error: Unknown config option "preset" with value "jest-react-native". This is either a typing error or another user mistake and fixing it will remove this message.
Using Jest CLI v13.2.3, jasmine2, babel-jest
 FAIL  __tests__/AuthorRequest-test.js (0s)
● Runtime Error
  - Error: Cannot find module 'throwOnWrongReactAPI' from 'react-native.js'
        at Resolver.resolveModule (node_modules/jest-cli/node_modules/jest-resolve/build/index.js:197:17)
        at eval (node_modules/react-native/Libraries/react-native/react-native.js:180:26)
        at Object.<anonymous> (node_modules/react-native/Libraries/react-native/react-native.js:189:4)
1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 2.238s)
npm ERR! Test failed.  See above for more details.

我的 .babelrc 文件包含:

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

1 回答 1

0

我有一种感觉,你使用了错误的 Jest 版本。你有:

"jest": "^14.1.0",
"jest-cli": "^13.1.0"

但似乎您已13.2.3使用以下方式安装npm -g

Using Jest CLI v13.2.3, jasmine2, babel-jest

首先,我认为您可以删除jest-cli并仅使用jest 14.1.0.

然后你可以test像这样更新你的脚本:

"scripts": {
  "test": "./node_modules/jest/bin/jest.js"
}

通过这种方式,您可以确保运行项目的本地 Jest 副本,因此现在应该显示:

Using Jest CLI v14.1.0, jasmine2, babel-jest

这样做并遵循您发布的官方文档,它应该是您需要的一切(不能肯定地说,因为您没有发布测试代码)。

于 2016-08-22T16:55:17.620 回答