在单元测试中捕获组件反应错误

问题描述 投票:0回答:1

无法测试React组件引发一些错误。

// react.js
function Test() {
    if (true)
        throw `Err msg`;
    return <div> abc </div>;
}
// react.spec.js
it('throw test', () => {
    let testwrapper = () => {
        act(() => {
            ReactDOM.render(<Test/>, HtmlContainer);
        });
    };
    expect(testwrapper).toThrow('Err msg');
});

expect(...).toThrow(...)能够匹配抛出的错误消息,但是终端上的结果显示以下错误:

Chrome Headless 81.0.4044.138 (Windows 10) tests of Test throw error FAILED
        Uncaught Err msg thrown

使用Karma,Jasmine运行测试>

无法测试React组件引发一些错误。 // react.js函数Test(){如果(true)抛出`Err msg`;返回

abc; } // react.spec.js it('throw ...
reactjs unit-testing testing jasmine karma-runner
1个回答
0
投票

问题出在这里;您应该在expect中定义函数。

© www.soinside.com 2019 - 2024. All rights reserved.