【发布时间】:2018-03-22 04:00:04
【问题描述】:
假设我正在使用jest --coverage 测试下面的 React 组件:
class MyComponent extends React.Component {
constructor(props) {
super(props)
if (props.invalid) {
throw new Error('invalid')
}
}
}
覆盖报告将显示throw new Error('invalid') 行未被覆盖。由于.not.toThrow() 似乎没有涵盖任何内容,因此我使用酶创建了以下测试:
const wrapper = shallow(
<MyComponent invalid />
)
it('should throw', () => {
function fn() {
if (wrapper.instance().props.invalid) {
throw new Error('invalid')
}
}
expect(fn).toThrow()
})
线路被覆盖!然而,测试本身因encountered a declaration exception 而失败 - 这意味着原始组件抛出了错误(应该如此)?
我用toThrow() 错了吗?
【问题讨论】:
-
不应该是
expect(fn()).toThrow() -
@AndreasKöberle no
标签: testing jestjs code-coverage enzyme throw