The Enzyme docs for Full DOM Rendering here contains the following example of spying on a lifecycle method with Sinon:
describe('<Foo />', () => {
it('calls componentDidMount', () => {
sinon.spy(Foo.prototype, 'componentDidMount');
const wrapper = mount(<Foo />);
expect(Foo.prototype.componentDidMount.calledOnce).to.equal(true);
});
});
What is the equivalent to this using mock functions from Jest?
I'm using Create-React-App, and would rather not include Sinon if the same can be achieved with Jest.
Here's what I'd expect the test to look like:
describe('<App />', () => {
it('calls componentDidMount', () => {
jest.fn(App.prototype, 'componentDidMount');
const wrapper = mount(<App />);
expect(App.prototype.componentDidMount.mock.calls.length).toBe(1);
});
});
In this case, App.prototype.componentDidMount
doesn't reference the same function spy as it would do with Sinon.
The Jest docs on how mock functions actually work are a bit limited. I've followed the discussion here around what jest.fn() is doing, but it seems it's not really equivalent to sinon.spy().
How can I replicate that test with Jest?