根据我的理解以及到目前为止我在各种答案中所读的内容,并非所有生命周期方法都应该在浅层渲染中运行。特别是 componentDidMount
但是,我注意到当我这样做
beforeEach(function(){ fakeComponentDidMount = sinon.stub(Component.prototype,'componentDidMount'); fakeComponentDidMount.callsFake(function(){} ); 包装=浅(<组件{... props} />); }); afterEach(function(){ fakeComponentDidMount.restore(); }); 它(称为 componentDidMount,函数(){ Expect(fakeComponentDidMount.call).to.equal(true); });测试通过。
那么,我在这里做错了还是我理解错了?
以供参考
解决方案是的,它在酶3.0 中。
github/airbnb/enzyme/blob/master/CHANGELOG.md#300
LifeCycleExperimental 以前是您必须在 shallow 上手动设置为true的选项,因为它现在已经稳定了,所以现在默认启用。 / p>
这比想要测试生命周期时不得不求助于 mount 好得多。
现在绝对没有借口不使用 shallow 进行单元测试:)...除了需要时, o测试裁判:(。
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
解决方案Yes it is in enzyme 3.0.
github/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 :(.
更多推荐
componentDidMount是否应该在Enzyme中以浅层渲染运行?
发布评论