showing results for - "jest mockclear"
Mirko
21 Jan 2021
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});
Andrés
08 Aug 2019
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});
queries leading to this page
jest spy mock resultjest spy on functionmockimplementation with various outputs jestmockclear jestjest restore mocksjest reset mocksjest restoreallmocks beforeeachmockreset jestmock jest fnrestore mock jestjest mock clearjest mockclearclear value of mockreturnvalue in jestjest utils mock restore original functionspying on a fucntion jestmock functions using mockreturnvalueonce in jestjest spy on a functionjest mock empty function typescriptjest clear mock callsspy on function jestjest fn resetjest when to reset mocksjest clearmocksmock restore jestjest mocmock resetjest clear mocksjest mock restore functionjest mock functino without spyjest mock restore not workingjest clear mock implementationjest mock errorclear mock calls jestjest mockclearjest mock dispatch clean clearundoing jest mockjest mock method return value in classjset reset calls on mocked modulejest spyon to mock a function calljest mock restorejest destroy mock jest spu returnsjest reset mock spyjest expect constructor to be calledjest diable spyjest fn 28jest restore all mocksmock restorejest mockimplementationjest mock restore statespy function jestjest mock require restorejest mock restorejest clear all mocksenzyme clear jest mockjest spy mock implementationundo mock jestmock resetmockimplementation promisereset jest mockjest mock resetreset jest mocksjest reset mockwhy do you have to restore mock jestmockimplementation examplejest mock resetting a mockspy a function jestjest set mock calls onejest restore mockrestore mockrestore jest 2cmockjest restore specific mockreset mocks jestundo a mock jestjest mock reset vs clearjest restoreallmocks vs resetallmocksclear mocksjest mock module restoreclear mocks jhestjest manually restore mockhow to reset jest fnjest clearmockjest clear function calljest spy on function if calledjest mock restore originalspyon mockrestore 28 29jest mock resolved value once vs mocked resolve valuemock resolved value enzymereset all mock in jestjest reset call countjest mock new instance jest mockjest reset mocks manuallyjest spy mockimplementationreset jest mockstop 3a jest fn 28 29 mockresolvedvalue 28 28 29jest fnjest fnjest mockimplementationoncejest mockrestorejest fn resolvesmocking new instance jestjest mockclear