Karma test in React app , failing with "TypeError: (0 , _expect2.default)(...).toExist is not a function"

Karma test in React app , failing with "TypeError: (0 , _expect2.default)(...).toExist is not a function"

我是 React 的新手,在我的应用程序(FilterItems 组件)中面临这个失败的测试问题。非常感谢任何指导或帮助。

这是我的简单 FilterItems 组件。

import React, { Component } from 'react';

class FilterItems extends React.Component {
  render () {
    return (
      <div className="filter-items">
        {this.props.data.map(item => (<div className="filter-item">{item.name}</div>))}
      </div>

    )
  }
}

export default FilterItems;

这是我正在 运行 进行的测试,它失败了 - 错误为“TypeError: (0 , _expect2.default)(...).toExist is不是函数 在上下文中。 (src/tests/FilterItems.test.js:57453:40)"

我的 FilterItems.test.js 下面的文件

import React from 'react';
import expect from 'expect';
import FilterItems from '../components/FilterItems';
import ReactTestUtils from 'react-dom/test-utils'; // ES6


describe('FilterItems', () => {
  it('should exist', () => {
    expect(FilterItems).toBeTruthy();
  });
});          


describe('FilterItems', function () {
  it('loads without problems', function () {
    var filterItems = ReactTestUtils.renderIntoDocument(<FilterItems
      data={ [1, 2, 3] }
      />);

  expect(filterItems).toExist();
  });
});

为了以防万一,下面是 karma.conf.js 文件

var webpackConfig = require('./webpack.config.js');

module.exports = function (config) {
  config.set({
    browsers: ['Chrome'],
    singleRun: true,
    frameworks: ['mocha'],
    files: [
      'src/tests/**/*.test.js'
    ],
    preprocessors: {
      'src/tests/**/*.test.js': ['webpack', 'sourcemap']
  },
  reporters: ['mocha'],
  client: {
    mocha: {
      timeout: '5000'
    }
  },
  webpack: webpackConfig,
  webpackServer: {
    noInfo: true
  }
});
};

出于某种原因,此函数不存在于从 expect 函数返回的对象中,并且文档有误。我建议使用 toBeTruthy 别名。