简体   繁体   English

componentDidMount应该在Enzyme中使用浅渲染运行吗?

[英]Is componentDidMount supposed to run with shallow rendering in Enzyme?

From my understanding and from what I have read so far in various answers, not all lifecycle methods are supposed to be run with shallow rendering. 根据我的理解和迄今为止我在各种答案中所阅读的内容,并非所有生命周期方法都应该使用浅渲染来运行。 Especially componentDidMount 特别是componentDidMount

However, I notice that when I do 但是,当我这样做时,我注意到了

  beforeEach(function () {
    fakeComponentDidMount = sinon.stub(Component.prototype, 'componentDidMount');
    fakeComponentDidMount.callsFake(function () {});
    wrapper = shallow(<Component {...props} />);
  });

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

  it('calls componentDidMount', function () {
    expect(fakeComponentDidMount.called).to.equal(true);
  });

the test passes. 测试通过。

So, am I doing something wrong here or have I understood something wrong? 那么,我在这里做错了还是我明白了什么?

For reference 以供参考

Yes it is in enzyme 3.0 . 是的,它是enzyme 3.0

https://github.com/airbnb/enzyme/blob/master/CHANGELOG.md#300 https://github.com/airbnb/enzyme/blob/master/CHANGELOG.md#300

LifeCycleExperimental which was previously an option that you had to manually set to true on shallow is now enabled by default because it is now stable. LifeCycleExperimental以前是一个必须在shallow手动设置为true的选项现在默认启用,因为它现在是稳定的。

This is much nicer than having to resort to mount when wanting to test lifecycles. 这比想要测试生命周期时不得不求助于mount更好。

There is absolutely no excuse to not use shallow for unit tests now :)... Well apart from when you need to test refs :(. 现在绝对没有理由不使用shallow单元测试:)...除了你需要测试参考时:(。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM