Skip to content

Commit 3e5bea3

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

1 file changed

Lines changed: 14 additions & 22 deletions

File tree

test/server/proxy-option.test.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ const proxyWithRouterAsObject = [
139139
describe("proxy option", () => {
140140
let proxyServer1;
141141
let proxyServer2;
142+
let consoleMock;
142143

143144
async function listenProxyServers() {
144145
const proxyApp1 = express();
@@ -837,12 +838,14 @@ describe("proxy option", () => {
837838
let customLogProvider;
838839

839840
beforeAll(async () => {
841+
consoleMock = jest.spyOn(console, "error");
842+
840843
customLogProvider = {
841844
log: jest.fn(),
842845
debug: jest.fn(),
843846
info: jest.fn(),
844847
warn: jest.fn(),
845-
error: jest.fn(),
848+
error: () => console.error("custom error log"),
846849
};
847850

848851
const compiler = webpack([config, config]);
@@ -869,6 +872,7 @@ describe("proxy option", () => {
869872
});
870873

871874
afterAll(async () => {
875+
consoleMock.mockRestore();
872876
await server.stop();
873877
await closeProxyServers();
874878
});
@@ -877,24 +881,18 @@ describe("proxy option", () => {
877881
it("respects a proxy option when a request path is matched", async () => {
878882
await req.get("/my-path");
879883

880-
expect(customLogProvider.error).toHaveBeenCalledTimes(1);
884+
expect(consoleMock).toHaveBeenCalledTimes(1);
885+
expect(consoleMock).toHaveBeenCalledWith("custom error log");
881886
});
882887
});
883888
});
884889

885890
describe("should work and respect the `infrastructureLogging.level` option", () => {
886891
let server;
887892
let req;
888-
let customLogProvider;
889893

890894
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-
};
895+
consoleMock = jest.spyOn(console, "error");
898896

899897
const compiler = webpack({
900898
...config,
@@ -907,7 +905,6 @@ describe("proxy option", () => {
907905
{
908906
context: "/my-path",
909907
target: "http://unknown:1234",
910-
logger: customLogProvider,
911908
},
912909
],
913910
port: port3,
@@ -923,6 +920,7 @@ describe("proxy option", () => {
923920
});
924921

925922
afterAll(async () => {
923+
consoleMock.mockRestore();
926924
await server.stop();
927925
await closeProxyServers();
928926
});
@@ -931,24 +929,17 @@ describe("proxy option", () => {
931929
it("respects a proxy option when a request path is matched", async () => {
932930
await req.get("/my-path");
933931

934-
expect(customLogProvider.error).toHaveBeenCalledTimes(1);
932+
expect(consoleMock).toHaveBeenCalledTimes(1);
935933
});
936934
});
937935
});
938936

939937
describe("should work and respect the `infrastructureLogging.level` option with `none` value", () => {
940938
let server;
941939
let req;
942-
let customLogProvider;
943940

944941
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-
};
942+
consoleMock = jest.spyOn(console, "error");
952943

953944
const compiler = webpack({
954945
...config,
@@ -961,7 +952,6 @@ describe("proxy option", () => {
961952
{
962953
context: "/my-path",
963954
target: "http://unknown:1234",
964-
logger: customLogProvider,
965955
},
966956
],
967957
port: port3,
@@ -975,14 +965,16 @@ describe("proxy option", () => {
975965
});
976966

977967
afterAll(async () => {
968+
consoleMock.mockRestore();
978969
await server.stop();
979970
});
980971

981972
describe("target", () => {
982973
it("respects a proxy option when a request path is matched", async () => {
983974
await req.get("/my-path");
984975

985-
expect(customLogProvider.error).toHaveBeenCalledTimes(0);
976+
// Since infrastructureLogging.level is set to "none", no error logs should be produced but http-proxy-middleware still calls the error logger though no show.
977+
expect(consoleMock).toHaveBeenCalledTimes(0);
986978
});
987979
});
988980
});

0 commit comments

Comments
 (0)