我有一个内部带有选择的组件
<select onChange={this.handleChange}>
{this.props.options.map(this.renderOption)}
</select>
以及以下功能
handleChange(e) {
const element = e.target;
this.props.onChange.call(null, e, {
value: element.value,
label: element.options[element.selectedIndex].textContent,
});
}
我在 Jest 和 Enzyme 中写了一个测试,像这样:
const onChange = jest.fn();
// ....
component.find('select').simulate('change');
expect(onChange).toHaveBeenCalled();
问题是我的%Funcs 覆盖率为 83.33%,因为它认为没有调用handleChange
函数。除此之外,所有其他覆盖范围(%Stmts,%Branch,%Lines)均为100%。这是一个酶/开玩笑的错误,还是我做错了什么?
PS:我尝试写一个虚拟测试,我会手动调用handleChange
,覆盖率达到100%。所以,这肯定与此有关。
编辑,澄清:正在调用handleChange
函数,测试有效。问题是覆盖范围不计算函数被调用。
答案 0 :(得分:1)
我发现了我的情况。
SelectComponent.defaultProps = {
options: [],
onChange: () => {},
};
这是因为默认的属性函数。当我进行单元测试时,我用模拟函数替换它,因此从不调用该函数,从而使得覆盖不计算它。