jest mock reset vs clear code example
Example 1: jest mock reset
test("mockFn.mockReset", () => {
const mockFn = jest.fn().mockImplementation(() => 43);
const MockClass = jest.fn();
new MockClass();
expect(mockFn()).toBe(43);
expect(mockFn.mock.calls).toHaveLength(1);
expect(MockClass.mock.instances).toHaveLength(1);
mockFn.mockReset();
MockClass.mockReset();
new MockClass();
expect(mockFn()).toBeUndefined();
expect(mockFn.mock.calls).toHaveLength(1);
expect(MockClass.mock.instances).toHaveLength(1);
});
Example 2: jest mockclear
test("mockFn.mockClear", () => {
const mockFn = jest.fn().mockImplementation(() => 42);
const MockClass = jest.fn();
new MockClass();
expect(mockFn()).toBe(42);
expect(mockFn.mock.calls).toHaveLength(1);
expect(MockClass.mock.instances).toHaveLength(1);
new MockClass();
expect(mockFn()).toBe(42);
expect(mockFn.mock.calls).toHaveLength(2);
expect(MockClass.mock.instances).toHaveLength(2);
mockFn.mockClear();
MockClass.mockClear();
new MockClass();
expect(mockFn()).toBe(42);
expect(mockFn.mock.calls).toHaveLength(1);
expect(MockClass.mock.instances).toHaveLength(1);
});