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

Dim*_*nis 17 javascript unit-testing reactjs enzyme

根据我的理解和迄今为止我在各种答案中所阅读的内容,并非所有生命周期方法都应该使用浅渲染来运行.特别componentDidMount

但是,当我这样做时,我注意到了

  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);
  });
Run Code Online (Sandbox Code Playgroud)

测试通过.

那么,我在这里做错了还是我明白了什么?

以供参考

Mar*_*son 24

是的它在enzyme 3.0.

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

LifeCycleExperimental以前是一个必须手动设置为true的选项shallow现在默认启用,因为它现在是稳定的.

这比mount想要测试生命周期时不得不求助更好.

现在绝对没有理由不shallow用于单元测试:))...除了你需要测试参考时:(.