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});
1test("mockFn.mockClear", () => {
2 const mockFn = jest.fn().mockImplementation(() => 42);
3 const MockClass = jest.fn();
4
5 new MockClass();
6 expect(mockFn()).toBe(42);
7
8 expect(mockFn.mock.calls).toHaveLength(1);
9 expect(MockClass.mock.instances).toHaveLength(1);
10
11 new MockClass();
12 expect(mockFn()).toBe(42);
13
14 expect(mockFn.mock.calls).toHaveLength(2);
15 expect(MockClass.mock.instances).toHaveLength(2);
16
17 mockFn.mockClear();
18 MockClass.mockClear();
19
20 new MockClass();
21 expect(mockFn()).toBe(42);
22
23 expect(mockFn.mock.calls).toHaveLength(1);
24 expect(MockClass.mock.instances).toHaveLength(1);
25
26});