reactjs - Karma test in React app , failing with "TypeError: (0 , _expect2.default)(...).toExist is not a function"
问题描述
I am new to React, and facing this failing test issue in my app (FilterItems component). Would greatly apprecitate any guidance or help.
Here's my simple FilterItems component.
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;
And here's the test I am running on it, which fails - with the error " TypeError: (0 , _expect2.default)(...).toExist is not a function at Context. (src/tests/FilterItems.test.js:57453:40)"
My FilterItems.test.js file below
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();
});
});
Just in case it helps, below is the karma.conf.js file
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
}
});
};
解决方案
由于某种原因,从期望函数返回的对象中不存在此函数,并且文档是错误的。我建议使用 toBeTruthy 别名。
推荐阅读
- image - 裁剪和旋转捕获的图片 Xamarin.Android 和 Xamarin.iOS
- google-cloud-platform - Kubernetes Engine 无法从非私有/GCR 存储库中提取映像
- tensorflow - 将 Darkflow tensorflow 模型移植到 Tensorflow Android 相机检测演示
- mongodb - MeteorJS 应用安全
- java - 实时自我监控 Java 进程的内存
- scala - AuthenticationException Neo4j Spark 流式传输
- angular - Angular 5 自动对焦
- java - SQLite 1:M 关系
- c# - 将 Umbraco IContent 转换为 JSON
- java - 使用套接字将图像从客户端发送到服务器