Skip to content

Commit 1dbf2ab

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

2 files changed

Lines changed: 42 additions & 30 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/ [ENOTFOUND] (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: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ 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+
}
150+
151+
function getConsoleErrorOutput(consoleSpy) {
152+
return consoleSpy.mock.calls
153+
.map((call) => call[0])
154+
.join("\n")
155+
.replaceAll(/127\.0\.0\.1:\d+/g, "127.0.0.1:<port>");
156+
}
157+
143158
async function listenProxyServers() {
144159
const proxyApp1 = express();
145160
const proxyApp2 = express();
@@ -834,16 +849,10 @@ describe("proxy option", () => {
834849
describe("should work and respect `logger` option", () => {
835850
let server;
836851
let req;
837-
let customLogProvider;
852+
let consoleSpy;
838853

839854
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-
};
855+
consoleSpy = jest.spyOn(console, "error").mockImplementation(() => {});
847856

848857
const compiler = webpack([config, config]);
849858

@@ -853,7 +862,7 @@ describe("proxy option", () => {
853862
{
854863
context: "/my-path",
855864
target: "http://unknown:1234",
856-
logger: customLogProvider,
865+
logger: console,
857866
},
858867
],
859868
port: port3,
@@ -869,6 +878,7 @@ describe("proxy option", () => {
869878
});
870879

871880
afterAll(async () => {
881+
consoleSpy.mockRestore();
872882
await server.stop();
873883
await closeProxyServers();
874884
});
@@ -877,24 +887,20 @@ describe("proxy option", () => {
877887
it("respects a proxy option when a request path is matched", async () => {
878888
await req.get("/my-path");
879889

880-
expect(customLogProvider.error).toHaveBeenCalledTimes(1);
890+
expect(getConsoleErrorOutput(consoleSpy)).toMatchSnapshot();
881891
});
882892
});
883893
});
884894

885895
describe("should work and respect the `infrastructureLogging.level` option", () => {
886896
let server;
887897
let req;
888-
let customLogProvider;
898+
let stderrSpy;
889899

890900
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-
};
901+
stderrSpy = jest
902+
.spyOn(process.stderr, "write")
903+
.mockImplementation(() => true);
898904

899905
const compiler = webpack({
900906
...config,
@@ -907,7 +913,6 @@ describe("proxy option", () => {
907913
{
908914
context: "/my-path",
909915
target: "http://unknown:1234",
910-
logger: customLogProvider,
911916
},
912917
],
913918
port: port3,
@@ -923,6 +928,7 @@ describe("proxy option", () => {
923928
});
924929

925930
afterAll(async () => {
931+
stderrSpy.mockRestore();
926932
await server.stop();
927933
await closeProxyServers();
928934
});
@@ -931,24 +937,20 @@ describe("proxy option", () => {
931937
it("respects a proxy option when a request path is matched", async () => {
932938
await req.get("/my-path");
933939

934-
expect(customLogProvider.error).toHaveBeenCalledTimes(1);
940+
expect(getStderrOutput(stderrSpy)).toMatchSnapshot();
935941
});
936942
});
937943
});
938944

939945
describe("should work and respect the `infrastructureLogging.level` option with `none` value", () => {
940946
let server;
941947
let req;
942-
let customLogProvider;
948+
let stderrSpy;
943949

944950
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-
};
951+
stderrSpy = jest
952+
.spyOn(process.stderr, "write")
953+
.mockImplementation(() => true);
952954

953955
const compiler = webpack({
954956
...config,
@@ -961,7 +963,6 @@ describe("proxy option", () => {
961963
{
962964
context: "/my-path",
963965
target: "http://unknown:1234",
964-
logger: customLogProvider,
965966
},
966967
],
967968
port: port3,
@@ -975,14 +976,15 @@ describe("proxy option", () => {
975976
});
976977

977978
afterAll(async () => {
979+
stderrSpy.mockRestore();
978980
await server.stop();
979981
});
980982

981983
describe("target", () => {
982984
it("respects a proxy option when a request path is matched", async () => {
983985
await req.get("/my-path");
984986

985-
expect(customLogProvider.error).toHaveBeenCalledTimes(0);
987+
expect(getStderrOutput(stderrSpy)).toMatchSnapshot();
986988
});
987989
});
988990
});

0 commit comments

Comments
 (0)