这里的完整DOM渲染的酶文档包含以下使用Sinon监视生命周期方法的示例:
describe('<Foo />', () => {
it('calls componentDidMount', () => {
sinon.spy(Foo.prototype, 'componentDidMount');
const wrapper = mount(<Foo />);
expect(Foo.prototype.componentDidMount.calledOnce).to.equal(true);
});
});
Run Code Online (Sandbox Code Playgroud)
使用Jest的模拟函数相当于什么?
我正在使用Create-React-App,如果使用Jest可以实现同样的目的,我宁愿不包括Sinon.
这是我期望测试的样子:
describe('<App />', () => {
it('calls componentDidMount', () => {
jest.fn(App.prototype, 'componentDidMount');
const wrapper = mount(<App />);
expect(App.prototype.componentDidMount.mock.calls.length).toBe(1);
});
});
Run Code Online (Sandbox Code Playgroud)
在这种情况下,App.prototype.componentDidMount不会引用与Sinon相同的函数间谍.
关于模拟函数实际工作方式的Jest文档有点受限.我在这里讨论了jest.fn()正在做什么,但它似乎并不等同于sinon.spy().
我怎样才能用Jest复制那个测试?