diff --git a/test/Compiler.test.js b/test/Compiler.test.js index ba7dfa617d6..8507090edac 100644 --- a/test/Compiler.test.js +++ b/test/Compiler.test.js @@ -6,6 +6,7 @@ const path = require("path"); const webpack = require("../"); const WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter"); const MemoryFs = require("memory-fs"); +const captureStdio = require("./helpers/captureStdio"); describe("Compiler", () => { jest.setTimeout(20000); @@ -515,32 +516,12 @@ describe("Compiler", () => { }); }); describe("infrastructure logging", () => { - const CONSOLE_METHODS = [ - "error", - "warn", - "info", - "log", - "debug", - "trace", - "profile", - "profileEnd", - "group", - "groupEnd", - "groupCollapsed" - ]; - const spies = {}; + let capture; beforeEach(() => { - for (const method of CONSOLE_METHODS) { - if (console[method]) { - spies[method] = jest.spyOn(console, method).mockImplementation(); - } - } + capture = captureStdio(process.stderr); }); afterEach(() => { - for (const method in spies) { - spies[method].mockRestore(); - delete spies[method]; - } + capture.restore(); }); class MyPlugin { apply(compiler) { @@ -574,25 +555,18 @@ describe("Compiler", () => { }); compiler.outputFileSystem = new MemoryFs(); compiler.run((err, stats) => { - expect(spies.group).toHaveBeenCalledTimes(1); - expect(spies.group).toHaveBeenCalledWith("[MyPlugin] Group"); - expect(spies.groupCollapsed).toHaveBeenCalledTimes(1); - expect(spies.groupCollapsed).toHaveBeenCalledWith( - "[MyPlugin] Collaped group" - ); - expect(spies.error).toHaveBeenCalledTimes(1); - expect(spies.error).toHaveBeenCalledWith(" [MyPlugin] Error"); - expect(spies.warn).toHaveBeenCalledTimes(1); - expect(spies.warn).toHaveBeenCalledWith(" [MyPlugin] Warning"); - expect(spies.info).toHaveBeenCalledTimes(1); - expect(spies.info).toHaveBeenCalledWith(" [MyPlugin] Info"); - expect(spies.log).toHaveBeenCalledTimes(3); - expect(spies.log).toHaveBeenCalledWith("[MyPlugin] Log"); - expect(spies.log).toHaveBeenCalledWith( - "[MyPlugin] Log inside collapsed group" - ); - expect(spies.debug).toHaveBeenCalledTimes(0); - expect(spies.groupEnd).toHaveBeenCalledTimes(2); + expect(capture.toString().replace(/[\d.]+ms/, "Xms")) + .toMatchInlineSnapshot(` +"<-> [MyPlugin] Group + [MyPlugin] Error + [MyPlugin] Warning + [MyPlugin] Info + [MyPlugin] Log + <-> [MyPlugin] Collaped group + [MyPlugin] Log inside collapsed group + [MyPlugin] Time: Xms +" +`); done(); }); }); @@ -612,26 +586,19 @@ describe("Compiler", () => { }); compiler.outputFileSystem = new MemoryFs(); compiler.run((err, stats) => { - expect(spies.group).toHaveBeenCalledTimes(1); - expect(spies.group).toHaveBeenCalledWith("[MyPlugin] Group"); - expect(spies.groupCollapsed).toHaveBeenCalledTimes(1); - expect(spies.groupCollapsed).toHaveBeenCalledWith( - "[MyPlugin] Collaped group" - ); - expect(spies.error).toHaveBeenCalledTimes(1); - expect(spies.error).toHaveBeenCalledWith(" [MyPlugin] Error"); - expect(spies.warn).toHaveBeenCalledTimes(1); - expect(spies.warn).toHaveBeenCalledWith(" [MyPlugin] Warning"); - expect(spies.info).toHaveBeenCalledTimes(1); - expect(spies.info).toHaveBeenCalledWith(" [MyPlugin] Info"); - expect(spies.log).toHaveBeenCalledTimes(3); - expect(spies.log).toHaveBeenCalledWith("[MyPlugin] Log"); - expect(spies.log).toHaveBeenCalledWith( - "[MyPlugin] Log inside collapsed group" - ); - expect(spies.debug).toHaveBeenCalledTimes(1); - expect(spies.debug).toHaveBeenCalledWith("[MyPlugin] Debug"); - expect(spies.groupEnd).toHaveBeenCalledTimes(2); + expect(capture.toString().replace(/[\d.]+ms/, "Xms")) + .toMatchInlineSnapshot(` +"<-> [MyPlugin] Group + [MyPlugin] Error + [MyPlugin] Warning + [MyPlugin] Info + [MyPlugin] Log + [MyPlugin] Debug + <-> [MyPlugin] Collaped group + [MyPlugin] Log inside collapsed group + [MyPlugin] Time: Xms +" +`); done(); }); }); @@ -650,14 +617,7 @@ describe("Compiler", () => { }); compiler.outputFileSystem = new MemoryFs(); compiler.run((err, stats) => { - expect(spies.group).toHaveBeenCalledTimes(0); - expect(spies.groupCollapsed).toHaveBeenCalledTimes(0); - expect(spies.error).toHaveBeenCalledTimes(0); - expect(spies.warn).toHaveBeenCalledTimes(0); - expect(spies.info).toHaveBeenCalledTimes(0); - expect(spies.log).toHaveBeenCalledTimes(0); - expect(spies.debug).toHaveBeenCalledTimes(0); - expect(spies.groupEnd).toHaveBeenCalledTimes(0); + expect(capture.toString()).toMatchInlineSnapshot(`""`); done(); }); });