Skip to content

Commit

Permalink
fix Compiler logging test
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Aug 1, 2019
1 parent 4c472ad commit b89853e
Showing 1 changed file with 30 additions and 70 deletions.
100 changes: 30 additions & 70 deletions test/Compiler.test.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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("<e> [MyPlugin] Error");
expect(spies.warn).toHaveBeenCalledTimes(1);
expect(spies.warn).toHaveBeenCalledWith("<w> [MyPlugin] Warning");
expect(spies.info).toHaveBeenCalledTimes(1);
expect(spies.info).toHaveBeenCalledWith("<i> [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
<e> [MyPlugin] Error
<w> [MyPlugin] Warning
<i> [MyPlugin] Info
[MyPlugin] Log
<-> [MyPlugin] Collaped group
[MyPlugin] Log inside collapsed group
<t> [MyPlugin] Time: Xms
"
`);
done();
});
});
Expand All @@ -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("<e> [MyPlugin] Error");
expect(spies.warn).toHaveBeenCalledTimes(1);
expect(spies.warn).toHaveBeenCalledWith("<w> [MyPlugin] Warning");
expect(spies.info).toHaveBeenCalledTimes(1);
expect(spies.info).toHaveBeenCalledWith("<i> [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
<e> [MyPlugin] Error
<w> [MyPlugin] Warning
<i> [MyPlugin] Info
[MyPlugin] Log
[MyPlugin] Debug
<-> [MyPlugin] Collaped group
[MyPlugin] Log inside collapsed group
<t> [MyPlugin] Time: Xms
"
`);
done();
});
});
Expand All @@ -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();
});
});
Expand Down

0 comments on commit b89853e

Please sign in to comment.