所以我开始对我的React-Native应用程序进行单元测试。我只是想尝试第一次测试,我立即得到错误。
以下是我的测试结果:
import 'react-native';
import React from 'react';
import Index from '../index.ios.js';
// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';
it('renders correctly', () => {
const tree = renderer.create(
<Index />
).toJSON();
expect(tree).toMatchSnapshot();
});
&#13;
这是我的包JSON
{
"name": "project",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
"build": "npm install && react-native link && react-native run-ios"
},
"dependencies": {
"anymatch": "^1.3.0",
"native-base": "^2.2.0",
"react": "16.0.0-alpha.12",
"react-native": "0.45.1",
"react-native-datepicker": "^1.6.0",
"react-native-elements": "^0.13.0",
"react-native-local-notifications": "^0.1.0",
"react-native-maps": "^0.15.2",
"react-native-notifications": "^1.1.11",
"react-native-sound": "^0.10.3",
"react-native-swipeout": "^2.1.3",
"react-native-vector-icons": "^4.2.0",
"react-navigation": "^1.0.0-beta.11",
"react-redux": "^5.0.5",
"redux": "^3.7.1",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"babel-jest": "20.0.3",
"babel-preset-react-native": "2.0.0",
"jest": "20.0.4",
"react-test-renderer": "16.0.0-alpha.12"
},
"jest": {
"preset": "react-native"
}
}
&#13;
我得到的错误是:
TypeError: Cannot read property 'projects' of undefined
at /Users/grantherman/Desktop/project/node_modules/jest/node_modules/jest-cli/build/cli/runCLI.js:106:25
at Generator.next (<anonymous>)
at step (/Users/grantherman/Desktop/project/node_modules/jest/node_modules/jest-cli/build/cli/runCLI.js:1:260)
at /Users/grantherman/Desktop/project/node_modules/jest/node_modules/jest-cli/build/cli/runCLI.js:1:490
at Promise (<anonymous>)
at /Users/grantherman/Desktop/project/node_modules/jest/node_modules/jest-cli/build/cli/runCLI.js:1:171
at module.exports (/Users/grantherman/Desktop/project/node_modules/jest/node_modules/jest-cli/build/cli/runCLI.js:139:50)
at Object.run (/Users/grantherman/Desktop/project/node_modules/jest/node_modules/jest-cli/build/cli/index.js:40:3)
at Object.<anonymous> (/Users/grantherman/Desktop/project/node_modules/jest/node_modules/jest-cli/bin/jest.js:14:25)
at Module._compile (module.js:569:30)
我完全不知道为什么。我环顾四周,没有人遇到过与此相同的错误。
答案 0 :(得分:0)
在jest
的{{1}}下,尝试添加package.json
"testEnvironment": "jsdom"
答案 1 :(得分:0)
我在你的packages.json中看不到jest-cli。我和reactjs有完全相同的问题。安装jest-cli(我只有开玩笑)解决了这个问题。