describe("スパイ",()=>{ test("1.関数をスパイする", () => { const addMock = jest.spyOn(math, "add"); //実装は変わらない expect(app.doAdd(1, 2)).toEqual(3); //しかし追跡できる expect(addMock).toHaveBeenCalledWith(1, 2); }); test("2.スパイしている関数の実装を変更して戻す", () => { const addMock = jest.spyOn(math, "add"); // 実装を変更する addMock.mockImplementation(() => "mock"); expect(app.doAdd(1, 2)).toEqual("mock"); // 元に戻す addMock.mockRestore(); expect(app.doAdd(1, 2)).toEqual(3); }); })