在一個完美的世界 代碼庫中,不需要操作全局對象,但是世界 代碼庫很混亂- 所以正在測試。
您要不惜一切代價避免一個測試影響另一個測試。無論測試的順序如何,或是否跳過某些測試,測試都應該有意義。
模擬值的簡單方法是將屬性設定為測試中所需的任何值。
只要您更改此特定測試擁有(已建立)的本機物件中的值就可以了:
describe("override properties of local objects", () => { it("works and is harmless", () => { const myArray = [1]; myArray.length = 0; expect(myArray).toHaveLength(0); }); it("does not affect the next test", () => { const myArray = [1]; expect(myArray).toHaveLength(1); }); });
如果你對全域物件這樣做,它會變得混亂:
describe("don't override properties of global objects", () => { it("works before the property is overridden", () => { expect(window.innerWidth).toBeGreaterThan(0); }); it("works, but is evil", () => { window.innerWidth = 0; expect(window.innerWidth).toBe(0); }); it("fails in the test after the property was overridden", () => { expect(() => { expect(window.innerWidth).toBeGreaterThan(0); //這就是 jest.replaceProperty() 的用途:
describe("use jest.replaceProperty() to override properties of global objects", () => { afterEach(() => { jest.restoreAllMocks(); }); it("works before the property is overridden", () => { expect(window.innerWidth).toBeGreaterThan(0); }); it("works and is harmless", () => { jest.replaceProperty(window, "innerWidth", 0); expect(window.innerWidth).toBe(0); }); it("does not affect the next test", () => { expect(window.innerWidth).toBeGreaterThan(0); }); });模擬方法
方法可以像屬性一樣被模擬。
describe("override methods of local objects using jest.fn()", () => { it("works and is harmless", () => { const mySet = new Set([1]); mySet.has = jest.fn().mockReturnValue(false); expect(mySet.has(1)).toBeFalsy(); }); it("does not affect the next test", () => { const mySet = new Set([1]); expect(mySet.has(1)).toBeTruthy(); }); });如果您在全域物件上使用 myObject.someFunction = jest.fn() ,您的測試可能會相互依賴並失去其意義:
describe("don't override methods of global objects using jest.fn()", () => { it("works before the method is overridden", () => { expect(document.getElementById("foo")).toBeNull(); }); it("works, but is evil", () => { const el = document.createElement("div"); document.getElementById = jest.fn().mockReturnValue(el); expect(document.getElementById("foo")).toBe(el); }); it("fails in the test after the property was overridden", () => { expect(() => { expect(document.getElementById("foo")).toBeNull(); //我們該如何模擬全域物件中的方法?這就是 jest.spyOn() 的優點:
describe("use jest.spyOn() to override methods of global objects", () => { afterEach(() => { jest.restoreAllMocks(); }); it("works before the method is overridden", () => { expect(document.getElementById("foo")).toBeNull(); }); it("works and is harmless", () => { const el = document.createElement("div"); jest.spyOn(document, "getElementById").mockReturnValue(el); expect(document.getElementById("foo")).toBe(el); }); it("does not affect the next test", () => { expect(document.getElementById("foo")).toBeNull(); }); });你必須清理
如果您想確保所有測試都發現系統處於相同(新鮮、乾淨)狀態,則需要在每次測試後恢復模擬的狀態。
最簡單的解決方案是設定restoreMocks配置屬性。
最直接的選擇是在 afterEach()
中呼叫 jest.restoreAllMocks()如何為所有測試模擬某些內容
有時您會想模擬文件中所有測試的內容。如果您在頂層或在describe()區塊中使用jest.spyOn()和jest.replaceProperty(),則在執行第一個測試後所有Mock將被重設。
在頂層,您可以安全地重寫屬性和方法,無需 jest.spyOn() 和 jest.replaceProperty()。
如果您只想為describe() 區塊模擬事物,則需要在beforeEach() 掛鉤中進行這些呼叫。
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3