Fri, 08/14/2020 - 21:07
Creating spies
sinon
const spy = sinon.spy();
Jest
const spy = jest.fn();
Check if spy is called
sinon
spy.mock.calls.length; // number;
or
spy.mock.calls.length; // number;
Jest
expect(spy).toHaveBeenCalled();
Check call count
sinon
spy.calledOnce; // boolean
spy.calledTwice; // boolean
spy.calledThrice; // boolean
spy.callCount; // number
Jest
spy.mock.calls.length; // number;
Checking arguments
sinon
// args[callIndex][argIndex]
spy.args[0][0];
// spy.calledWith(...args)
spy.calledWith('Age', 23)
Jest
// mock.calls[call][argIdx]
spy.mock.calls[0][0];
expect(spy).toHaveBeenCalledWith(1, 'Hey');
expect(spy).toHaveBeenLastCalledWith(1, 'Hey');
// expect(spy).toHaveBeenCalledWith(argumentCondition);
expect(spy).toHaveBeenCalledWith(expect.anything());
expect(spy).toHaveBeenCalledWith(expect.any(constructor));
expect(spy).toHaveBeenCalledWith(expect.arrayContaining([ values ]));
expect(spy).toHaveBeenCalledWith(expect.objectContaining({ props }));
expect(spy).toHaveBeenCalledWith(expect.stringContaining(string));
expect(spy).toHaveBeenCalledWith(expect.stringMatching(regexp));
Authored by