2017-06-09 15 views
2

我不知道是否有到禁用控制檯錯誤一個更好的辦法一個特定玩笑測試(即恢復原來的控制檯前/每次測試後)。玩笑:最好禁用內部單元控制檯的方式測試

這是我目前的做法:

describe("Some description",() => { 
    let consoleSpy; 

    beforeEach(() => { 
    if (typeof consoleSpy === "function") { 
     consoleSpy.mockRestore(); 
    } 
    }); 

    test("Some test that should not output errors to jest console",() => { 
    expect.assertions(2); 

    consoleSpy = jest.spyOn(console, "error").mockImplementation(); 

    // some function that uses console error 
    expect(someFunction).toBe("X"); 
    expect(consoleSpy).toHaveBeenCalled(); 
    }); 

    test("Test that has console available",() => { 
    // shows up during jest watch test, just as intended 
    console.error("test"); 
    }); 
}); 

是否有完成同樣的事情的更清潔的方式? 我想避免spyOn,但mockRestore只能用於

謝謝!

回答

4

由於每個測試文件都在其自己的線程中運行,因此如果要在一個文件中禁用所有測試,則無需將其恢復。出於同樣的原因,您也可以直接寫

​​
+0

謝謝您關於此事的信息。它確實是有道理的:) 我正在尋找一種方法,使其只在特定的測試中進行,而無需恢復它(我最初認爲這是默認情況下的行爲),但我猜之前每個人都會這樣做。 – Apidcloud