Skip to content

Commit 1409bcc

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

1 file changed

Lines changed: 13 additions & 30 deletions

File tree

test/server/proxy-option.test.js

Lines changed: 13 additions & 30 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();
@@ -834,16 +835,9 @@ describe("proxy option", () => {
834835
describe("should work and respect `logger` option", () => {
835836
let server;
836837
let req;
837-
let customLogProvider;
838838

839839
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-
};
840+
consoleMock = jest.spyOn(console, "error");
847841

848842
const compiler = webpack([config, config]);
849843

@@ -853,7 +847,7 @@ describe("proxy option", () => {
853847
{
854848
context: "/my-path",
855849
target: "http://unknown:1234",
856-
logger: customLogProvider,
850+
logger: console,
857851
},
858852
],
859853
port: port3,
@@ -869,6 +863,7 @@ describe("proxy option", () => {
869863
});
870864

871865
afterAll(async () => {
866+
consoleMock.mockRestore();
872867
await server.stop();
873868
await closeProxyServers();
874869
});
@@ -877,24 +872,18 @@ describe("proxy option", () => {
877872
it("respects a proxy option when a request path is matched", async () => {
878873
await req.get("/my-path");
879874

880-
expect(customLogProvider.error).toHaveBeenCalledTimes(1);
875+
expect(consoleMock).toHaveBeenCalledTimes(1);
876+
expect(consoleMock).toHaveBeenCalledWith("custom error log");
881877
});
882878
});
883879
});
884880

885881
describe("should work and respect the `infrastructureLogging.level` option", () => {
886882
let server;
887883
let req;
888-
let customLogProvider;
889884

890885
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-
};
886+
consoleMock = jest.spyOn(console, "error");
898887

899888
const compiler = webpack({
900889
...config,
@@ -907,7 +896,6 @@ describe("proxy option", () => {
907896
{
908897
context: "/my-path",
909898
target: "http://unknown:1234",
910-
logger: customLogProvider,
911899
},
912900
],
913901
port: port3,
@@ -923,6 +911,7 @@ describe("proxy option", () => {
923911
});
924912

925913
afterAll(async () => {
914+
consoleMock.mockRestore();
926915
await server.stop();
927916
await closeProxyServers();
928917
});
@@ -931,24 +920,17 @@ describe("proxy option", () => {
931920
it("respects a proxy option when a request path is matched", async () => {
932921
await req.get("/my-path");
933922

934-
expect(customLogProvider.error).toHaveBeenCalledTimes(1);
923+
expect(consoleMock).toHaveBeenCalledTimes(1);
935924
});
936925
});
937926
});
938927

939928
describe("should work and respect the `infrastructureLogging.level` option with `none` value", () => {
940929
let server;
941930
let req;
942-
let customLogProvider;
943931

944932
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-
};
933+
consoleMock = jest.spyOn(console, "error");
952934

953935
const compiler = webpack({
954936
...config,
@@ -961,7 +943,6 @@ describe("proxy option", () => {
961943
{
962944
context: "/my-path",
963945
target: "http://unknown:1234",
964-
logger: customLogProvider,
965946
},
966947
],
967948
port: port3,
@@ -975,14 +956,16 @@ describe("proxy option", () => {
975956
});
976957

977958
afterAll(async () => {
959+
consoleMock.mockRestore();
978960
await server.stop();
979961
});
980962

981963
describe("target", () => {
982964
it("respects a proxy option when a request path is matched", async () => {
983965
await req.get("/my-path");
984966

985-
expect(customLogProvider.error).toHaveBeenCalledTimes(0);
967+
// 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.
968+
expect(consoleMock).toHaveBeenCalledTimes(0);
986969
});
987970
});
988971
});

0 commit comments

Comments
 (0)