Skip to content

Commit ef8353d

Browse files
committed
test: mock console.error for proxy option tests and verify error logging
1 parent 0a181ee commit ef8353d

2 files changed

Lines changed: 45 additions & 31 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`proxy option should work and respect \`logger\` option target respects a proxy option when a request path is matched 1`] = `"[HPM] Error occurred while proxying request %s to %s [%s] (%s)"`;
4+
5+
exports[`proxy option should work and respect the \`infrastructureLogging.level\` option target respects a proxy option when a request path is matched 1`] = `
6+
"<e> [webpack-dev-server] [HPM] Error occurred while proxying request 127.0.0.1:<port>/my-path to http://unknown:1234/ [<DNS_ERROR>] (https://nodejs.org/api/errors.html#errors_common_system_errors)
7+
"
8+
`;
9+
10+
exports[`proxy option should work and respect the \`infrastructureLogging.level\` option with \`none\` value target respects a proxy option when a request path is matched 1`] = `""`;

test/server/proxy-option.test.js

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,23 @@ describe("proxy option", () => {
140140
let proxyServer1;
141141
let proxyServer2;
142142

143+
function getStderrOutput(stderrSpy) {
144+
return stderrSpy.mock.calls
145+
.map((call) => call[0])
146+
.filter((output) => !output.includes("DeprecationWarning"))
147+
.join("")
148+
.replaceAll(/127\.0\.0\.1:\d+/g, "127.0.0.1:<port>")
149+
.replaceAll(/\[ENOTFOUND\]|\[EAI_AGAIN\]/g, "[<DNS_ERROR>]");
150+
}
151+
152+
function getConsoleErrorOutput(consoleSpy) {
153+
return consoleSpy.mock.calls
154+
.map((call) => call[0])
155+
.join("\n")
156+
.replaceAll(/127\.0\.0\.1:\d+/g, "127.0.0.1:<port>")
157+
.replaceAll(/\[ENOTFOUND\]|\[EAI_AGAIN\]/g, "[<DNS_ERROR>]");
158+
}
159+
143160
async function listenProxyServers() {
144161
const proxyApp1 = express();
145162
const proxyApp2 = express();
@@ -834,16 +851,10 @@ describe("proxy option", () => {
834851
describe("should work and respect `logger` option", () => {
835852
let server;
836853
let req;
837-
let customLogProvider;
854+
let consoleSpy;
838855

839856
beforeAll(async () => {
840-
customLogProvider = {
841-
log: jest.fn(),
842-
debug: jest.fn(),
843-
info: jest.fn(),
844-
warn: jest.fn(),
845-
error: jest.fn(),
846-
};
857+
consoleSpy = jest.spyOn(console, "error").mockImplementation(() => {});
847858

848859
const compiler = webpack([config, config]);
849860

@@ -853,7 +864,7 @@ describe("proxy option", () => {
853864
{
854865
context: "/my-path",
855866
target: "http://unknown:1234",
856-
logger: customLogProvider,
867+
logger: console,
857868
},
858869
],
859870
port: port3,
@@ -869,6 +880,7 @@ describe("proxy option", () => {
869880
});
870881

871882
afterAll(async () => {
883+
consoleSpy.mockRestore();
872884
await server.stop();
873885
await closeProxyServers();
874886
});
@@ -877,28 +889,24 @@ describe("proxy option", () => {
877889
it("respects a proxy option when a request path is matched", async () => {
878890
await req.get("/my-path");
879891

880-
expect(customLogProvider.error).toHaveBeenCalledTimes(1);
892+
expect(getConsoleErrorOutput(consoleSpy)).toMatchSnapshot();
881893
});
882894
});
883895
});
884896

885897
describe("should work and respect the `infrastructureLogging.level` option", () => {
886898
let server;
887899
let req;
888-
let customLogProvider;
900+
let stderrSpy;
889901

890902
beforeAll(async () => {
891-
customLogProvider = {
892-
log: jest.fn(),
893-
debug: jest.fn(),
894-
info: jest.fn(),
895-
warn: jest.fn(),
896-
error: jest.fn(),
897-
};
903+
stderrSpy = jest
904+
.spyOn(process.stderr, "write")
905+
.mockImplementation(() => true);
898906

899907
const compiler = webpack({
900908
...config,
901-
infrastructureLogging: { level: "error" },
909+
infrastructureLogging: { colors: false, level: "error" },
902910
});
903911

904912
server = new Server(
@@ -907,7 +915,6 @@ describe("proxy option", () => {
907915
{
908916
context: "/my-path",
909917
target: "http://unknown:1234",
910-
logger: customLogProvider,
911918
},
912919
],
913920
port: port3,
@@ -923,6 +930,7 @@ describe("proxy option", () => {
923930
});
924931

925932
afterAll(async () => {
933+
stderrSpy.mockRestore();
926934
await server.stop();
927935
await closeProxyServers();
928936
});
@@ -931,24 +939,20 @@ describe("proxy option", () => {
931939
it("respects a proxy option when a request path is matched", async () => {
932940
await req.get("/my-path");
933941

934-
expect(customLogProvider.error).toHaveBeenCalledTimes(1);
942+
expect(getStderrOutput(stderrSpy)).toMatchSnapshot();
935943
});
936944
});
937945
});
938946

939947
describe("should work and respect the `infrastructureLogging.level` option with `none` value", () => {
940948
let server;
941949
let req;
942-
let customLogProvider;
950+
let stderrSpy;
943951

944952
beforeAll(async () => {
945-
customLogProvider = {
946-
log: jest.fn(),
947-
debug: jest.fn(),
948-
info: jest.fn(),
949-
warn: jest.fn(),
950-
error: jest.fn(),
951-
};
953+
stderrSpy = jest
954+
.spyOn(process.stderr, "write")
955+
.mockImplementation(() => true);
952956

953957
const compiler = webpack({
954958
...config,
@@ -961,7 +965,6 @@ describe("proxy option", () => {
961965
{
962966
context: "/my-path",
963967
target: "http://unknown:1234",
964-
logger: customLogProvider,
965968
},
966969
],
967970
port: port3,
@@ -975,14 +978,15 @@ describe("proxy option", () => {
975978
});
976979

977980
afterAll(async () => {
981+
stderrSpy.mockRestore();
978982
await server.stop();
979983
});
980984

981985
describe("target", () => {
982986
it("respects a proxy option when a request path is matched", async () => {
983987
await req.get("/my-path");
984988

985-
expect(customLogProvider.error).toHaveBeenCalledTimes(0);
989+
expect(getStderrOutput(stderrSpy)).toMatchSnapshot();
986990
});
987991
});
988992
});

0 commit comments

Comments
 (0)