1test("mockFn.mockReset", () => {
2 const mockFn = jest.fn().mockImplementation(() => 43);
3 const MockClass = jest.fn();
4
5 new MockClass();
6 expect(mockFn()).toBe(43);
7
8 expect(mockFn.mock.calls).toHaveLength(1);
9 expect(MockClass.mock.instances).toHaveLength(1);
10
11 mockFn.mockReset();
12 MockClass.mockReset();
13
14 new MockClass();
15 expect(mockFn()).toBeUndefined();
16
17 expect(mockFn.mock.calls).toHaveLength(1);
18 expect(MockClass.mock.instances).toHaveLength(1);
19});
1test("mock.calls", () => {
2 const mockFn = jest.fn();
3 mockFn(1, 2);
4
5 expect(mockFn.mock.calls).toEqual([[1, 2]]);
6});
1// equvalent to mockReset
2
3test("mockFn.mockRestore", () => {
4 const StringUtils = {
5 toUpperCase(arg) {
6 return arg && arg.toUpperCase();
7 }
8 };
9
10 const spy = jest.spyOn(StringUtils, "toUpperCase").mockImplementation(() => "MOCK");
11
12 expect(StringUtils.toUpperCase("arg")).toBe("MOCK");
13 expect(spy).toHaveBeenCalledTimes(1);
14 expect(jest.isMockFunction(StringUtils.toUpperCase)).toBeTruthy();
15
16 spy.mockRestore();
17
18 expect(spy("arg")).toBeUndefined();
19 expect(jest.isMockFunction(StringUtils.toUpperCase)).not.toBeTruthy();
20 expect(StringUtils.toUpperCase("arg")).toBe("ARG");
21 expect(spy).toHaveBeenCalledTimes(1);
22
23});