Answers for "jest mock restore"

2

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);
});
Posted by: Guest on November-20-2020
0

jest mock restore

// equvalent to mockReset

test("mockFn.mockRestore", () => {
  const StringUtils = {
    toUpperCase(arg) {
      return arg && arg.toUpperCase();
    }
  };

  const spy = jest.spyOn(StringUtils, "toUpperCase").mockImplementation(() => "MOCK");

  expect(StringUtils.toUpperCase("arg")).toBe("MOCK");
  expect(spy).toHaveBeenCalledTimes(1);
  expect(jest.isMockFunction(StringUtils.toUpperCase)).toBeTruthy();

  spy.mockRestore();

  expect(spy("arg")).toBeUndefined();
  expect(jest.isMockFunction(StringUtils.toUpperCase)).not.toBeTruthy();
  expect(StringUtils.toUpperCase("arg")).toBe("ARG");
  expect(spy).toHaveBeenCalledTimes(1);
 
});
Posted by: Guest on November-20-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language