使用React Native项目。我试图用Jest测试一个更大的组件。我得到这个测试无法在未定义的属性上运行错误,这没有任何意义,因为几行之后它说所有的测试运行正常。
我已经尝试删除我的节点模块并再次运行npm install。
您可以在此处看到错误:
并在此进行测试:
import React from 'react';
import ReactNative, {
Image,
Text,
View
} from 'react-native';
import renderer from 'react-test-renderer';
import TeamDetail from '../../app/scenes/TeamDetail/TeamDetail';
const mockFn = () => 'THIS IS A MOCK!!!!';
describe('<TeamDetail />', () => {
it('renders correctly', () => {
const team = {};
const captains = {
count: 1,
items: [],
links: { next: 'next link', prev: 'prev link', },
};
const leagueTeamCount = 1;
const games = {
count: 1,
items: [],
links: { next: 'next link', prev: 'prev link', },
};
const roster = {
count: 1,
items: [],
links: { next: 'next link', prev: 'prev link', },
};
const fetchPendingPlayers = mockFn;
const pendingPlayers = {
count: 1,
items: [],
links: { next: 'next link', prev: 'prev link', },
};
const updateTeam = mockFn;
const addPlayerToTeam = mockFn;
const addCaptainToTeam = mockFn;
const approvePlayer = mockFn;
const canManageTeam = true;
const canMessage = true;
const clearSearchResults = mockFn;
const fetchTeamGames = mockFn;
const fetchTeamPlayers = mockFn;
const fetchUserProfile = mockFn;
const tree = renderer.create(
<TeamDetail
team={team}
captains={captains}
leagueTeamCount={leagueTeamCount}
games={games}
roster={roster}
fetchPendingPlayers={fetchPendingPlayers}
pendingPlayers={pendingPlayers}
updateTeam={updateTeam}
addPlayerToTeam={addPlayerToTeam}
addCaptainToTeam={addCaptainToTeam}
approvePlayer={approvePlayer}
canManageTeam={canManageTeam}
canMessage={canMessage}
clearSearchResults={clearSearchResults}
fetchTeamGames={fetchTeamGames}
fetchTeamPlayers={fetchTeamPlayers}
fetchUserProfile={fetchUserProfile}
searchResults={searchResults}
searchUsers={searchUsers}
setLocationAlert={setLocationAlert}
viewTeamMessageFeed={viewTeamMessageFeed}
/>
).toJSON();
expect(tree).toMatchSnapshot();
});
});
,这是package.json
{
"name": "APPNAME",
"version": "0.0.1",
"private": true,
"scripts": {
"flow": "flow",
"jest": "jest",
"lint": "eslint . --ext .js --ext .jsx --ignore-path .gitignore --ignore-path .eslintignore --cache",
"packager": "remotedev-debugger --port 8000 --injectserver && node_modules/react-native/packager/packager.sh",
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "mocha --require babel-register $(find app/tests -name \"*_test.js\")",
"bundle-android": "react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output android/app/src/main/assets/index.android.bundle --sourcemap-output android/app/src/main/assets/index.android.map --assets-dest android/app/src/main/res/"
},
"pre-commit": [
"lint",
"flow",
"test",
"jest"
],
"dependencies": {
"apsl-react-native-button": "^3.0.2",
"bugsnag-react-native": "^1.2.1",
"flow": "^0.2.3",
"flow-typed": "^2.0.0",
"immutable": "^3.8.1",
"jsrsasign": "^6.2.0",
"jwt-decode": "^2.1.0",
"momentjs": "^2.0.0",
"phoenix": "^1.2.1",
"ramda": "^0.22.1",
"react": "~15.4.0-rc.4",
"react-native": "0.40.0",
"react-native-button": "^1.7.1",
"react-native-drawer": "^2.3.0",
"react-native-experimental-navigation": "^0.26.10",
"react-native-fbsdk": "0.5",
"react-native-fetch-blob": "v0.10.2-beta.8",
"react-native-geocoder": "https://github.com/smartlogic/react-native-geocoder.git",
"react-native-image-picker": "0.25",
"react-native-invertible-scroll-view": "^1.0.0",
"react-native-router-flux": "^3.36.0",
"react-redux": "^4.4.5",
"redux": "^3.6.0",
"redux-action-buffer": "^1.0.1",
"redux-persist": "^4.0.0-alpha7",
"redux-saga": "^0.12.1"
},
"jest": {
"preset": "jest-react-native"
},
"devDependencies": {
"babel-eslint": "^7.1.0",
"babel-jest": "16.0.0",
"babel-plugin-ramda": "^1.1.6",
"babel-plugin-syntax-flow": "^6.18.0",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-preset-react-native": "1.9.0",
"babel-register": "^6.18.0",
"chai": "^3.5.0",
"eslint": "^3.9.1",
"eslint-plugin-flowtype": "^2.25.0",
"eslint-plugin-react": "^6.4.1",
"eslint-plugin-react-native": "^2.0.0",
"flow-bin": "0.38",
"jest": "^18.1.0",
"jest-cli": "^19.0.2",
"jest-react-native": "^17.1.0",
"jsverify": "^0.7.4",
"mocha": "^3.2.0",
"pre-commit": "^1.1.3",
"react-test-renderer": "^15.4.2",
"remote-redux-devtools": "^0.5.7",
"remote-redux-devtools-on-debugger": "^0.7.0"
}
}
答案 0 :(得分:0)
尝试同时安装jest和jest babel最新版本
npm install --save-dev jest
npm install --save-dev babel-jest regenerator-runtime
重新运行测试,它适用于我