Is componentDidMount supposed to run with shallow rendering in Enzyme?

前提是你 提交于 2021-02-07 11:17:47

问题


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

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


回答1:


Yes it is in enzyme 3.0.

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.

This is much nicer than having to resort to mount when wanting to test lifecycles.

There is absolutely no excuse to not use shallow for unit tests now :)... Well apart from when you need to test refs :(.



来源:https://stackoverflow.com/questions/47309585/is-componentdidmount-supposed-to-run-with-shallow-rendering-in-enzyme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!