describe("スパイ",()=>{ test("スパイは基本のfn()の組み合わせ", () => { // オリジナル実装をバックアップ const originalAdd = math.add; // オリジナル実装でモックする math.add = jest.fn(originalAdd); // オリジナル実装のスパイができるようになる expect(app.doAdd(1, 2)).toEqual(3); expect(math.add).toHaveBeenCalledWith(1, 2); // 実装を変更する math.add.mockImplementation(() => "mock"); expect(app.doAdd(1, 2)).toEqual("mock"); expect(math.add).toHaveBeenCalledWith(1, 2); // オリジナル実装に戻す math.add = originalAdd; expect(app.doAdd(1, 2)).toEqual(3); }) })