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