javascript - 测试套件无法运行 TypeError : Cannot read property 'bind' of undefined

标签 javascript react-native jestjs

从事 React Native 项目。我正在尝试使用 Jest 测试其中一个较大的组件。我收到此测试运行失败错误,属性未定义,这没有任何意义,因为几行之后它说所有测试都运行良好。

我已经尝试删除我的节点模块并再次运行 npm install。

你可以在这里看到错误:

enter image description here

和这里的测试:

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"
  }
}

最佳答案

尝试同时安装 jest 和 jest babel 最新版本

npm install --save-dev jest

npm install --save-dev babel-jest regenerator-runtime

然后重新运行测试,它对我有用

关于javascript - 测试套件无法运行 TypeError : Cannot read property 'bind' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42937962/

相关文章:

javascript - 如何使 Math.random 数组中的每个第 9 个元素都是相同的元素?[javascript]

javascript - Backbone.Collection 获取第一个 n 作为新集合

android - 尝试在 android 上使用 react-native-fbsdk 时出错

javascript - 具有独立状态的动态生成的复选框 React-Native

javascript - React 中如何模拟组件

mocking - Jest 模拟全局 navigator.onLine

javascript - 模拟调用 'upvote' 的效果时出现异常。类型错误 : undefined is not a function

javascript - 如何在不使用双引号的情况下以逗号分隔文本,同时保留引号?

javascript - 调用 AppRegistry.registerComponent 失败

javascript - 如何在 Jest 中模拟 const 方法?