【问题标题】:Enzyme check if component mounted without errors on prop types酶检查组件是否在道具类型上安装时没有错误
【发布时间】:2017-05-26 10:15:17
【问题描述】:

我正在为组件创建集成测试以测试其接口,并且我正在安装它

const wrapper = shallow(<Component boolVal={()=>{}} />

Component 中,我已将boolVal 属性标记为布尔值,并且在安装时我收到了预期的警告:

`Warning: Failed prop type: Invalid prop `boolVal` of type `function` supplied to `Component`, expected `boolean`.`

这是正确的,但我希望测试失败。

请问如何存档?

【问题讨论】:

  • 你可以查看 React 本身 tests 这个功能是如何实现的。对console.error()进行间谍活动。

标签: javascript reactjs mocha.js enzyme


【解决方案1】:

根据 Dmitri 的回答,这是一个监视 console.error 的测试。当 props 验证失败时测试失败:

describe.only('Test Component', () => {

    const sandbox = sinon.sandbox.create();

    beforeEach(function() {
        sandbox.spy(console, 'error');
    });

    it('mounts', function () {
        shallow(
            <Component boolVal={()=>{}} />
        );
        assert.isFalse(console.error.called);
    });

    afterEach(function() {
        sandbox.restore();
    });
});

【讨论】:

    猜你喜欢
    • 2018-06-15
    • 1970-01-01
    • 2018-12-16
    • 2021-03-14
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多