Skip to content

Commit 7176b4f

Browse files
committed
Clean up logging tests
Remove unused consoleErrorSpy and use vi.stubGlobal() consistently: - Remove consoleErrorSpy from beforeEach/afterEach (never asserted) - Replace direct global.fetch assignments with vi.stubGlobal('fetch', ...) - Aligns with outer suite's mocking pattern for easier test cleanup
1 parent 6fe7cac commit 7176b4f

File tree

1 file changed

+126
-105
lines changed

1 file changed

+126
-105
lines changed

tests/unit/client.test.ts

Lines changed: 126 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -867,19 +867,16 @@ describe("McpdClient", () => {
867867

868868
describe("logging", () => {
869869
let consoleWarnSpy: ReturnType<typeof vi.spyOn>;
870-
let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
871870
let originalLogLevel: string | undefined;
872871

873872
beforeEach(() => {
874873
consoleWarnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
875-
consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
876874
// Save original log level.
877875
originalLogLevel = process.env.MCPD_LOG_LEVEL;
878876
});
879877

880878
afterEach(() => {
881879
consoleWarnSpy.mockRestore();
882-
consoleErrorSpy.mockRestore();
883880
// Restore original log level.
884881
if (originalLogLevel === undefined) {
885882
delete process.env.MCPD_LOG_LEVEL;
@@ -893,18 +890,21 @@ describe("McpdClient", () => {
893890
// This means the default logger will be a noop.
894891
delete process.env.MCPD_LOG_LEVEL;
895892

896-
global.fetch = createFetchMock({
897-
[API_PATHS.SERVERS]: ["healthy", "unhealthy"],
898-
[API_PATHS.HEALTH_ALL]: {
899-
servers: [
900-
{ name: "healthy", status: "ok" },
901-
{ name: "unhealthy", status: "timeout" }, // Generates warning if logging enabled.
902-
],
903-
},
904-
[API_PATHS.SERVER_TOOLS("healthy")]: {
905-
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
906-
},
907-
});
893+
vi.stubGlobal(
894+
"fetch",
895+
createFetchMock({
896+
[API_PATHS.SERVERS]: ["healthy", "unhealthy"],
897+
[API_PATHS.HEALTH_ALL]: {
898+
servers: [
899+
{ name: "healthy", status: "ok" },
900+
{ name: "unhealthy", status: "timeout" }, // Generates warning if logging enabled.
901+
],
902+
},
903+
[API_PATHS.SERVER_TOOLS("healthy")]: {
904+
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
905+
},
906+
}),
907+
);
908908

909909
const newClient = new McpdClient({
910910
apiEndpoint: "http://localhost:8090",
@@ -918,18 +918,21 @@ describe("McpdClient", () => {
918918
// Set MCPD_LOG_LEVEL to enable logging via environment variable.
919919
process.env.MCPD_LOG_LEVEL = "warn";
920920

921-
global.fetch = createFetchMock({
922-
[API_PATHS.SERVERS]: ["healthy", "unhealthy"],
923-
[API_PATHS.HEALTH_ALL]: {
924-
servers: [
925-
{ name: "healthy", status: "ok" },
926-
{ name: "unhealthy", status: "timeout" },
927-
],
928-
},
929-
[API_PATHS.SERVER_TOOLS("healthy")]: {
930-
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
931-
},
932-
});
921+
vi.stubGlobal(
922+
"fetch",
923+
createFetchMock({
924+
[API_PATHS.SERVERS]: ["healthy", "unhealthy"],
925+
[API_PATHS.HEALTH_ALL]: {
926+
servers: [
927+
{ name: "healthy", status: "ok" },
928+
{ name: "unhealthy", status: "timeout" },
929+
],
930+
},
931+
[API_PATHS.SERVER_TOOLS("healthy")]: {
932+
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
933+
},
934+
}),
935+
);
933936

934937
const newClient = new McpdClient({
935938
apiEndpoint: "http://localhost:8090",
@@ -942,18 +945,21 @@ describe("McpdClient", () => {
942945
});
943946

944947
it("should warn when server is unhealthy", async () => {
945-
global.fetch = createFetchMock({
946-
[API_PATHS.SERVERS]: ["healthy", "unhealthy"],
947-
[API_PATHS.HEALTH_ALL]: {
948-
servers: [
949-
{ name: "healthy", status: "ok" },
950-
{ name: "unhealthy", status: "timeout" },
951-
],
952-
},
953-
[API_PATHS.SERVER_TOOLS("healthy")]: {
954-
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
955-
},
956-
});
948+
vi.stubGlobal(
949+
"fetch",
950+
createFetchMock({
951+
[API_PATHS.SERVERS]: ["healthy", "unhealthy"],
952+
[API_PATHS.HEALTH_ALL]: {
953+
servers: [
954+
{ name: "healthy", status: "ok" },
955+
{ name: "unhealthy", status: "timeout" },
956+
],
957+
},
958+
[API_PATHS.SERVER_TOOLS("healthy")]: {
959+
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
960+
},
961+
}),
962+
);
957963

958964
const customWarn = vi.fn();
959965
const loggerClient = new McpdClient({
@@ -969,17 +975,20 @@ describe("McpdClient", () => {
969975
});
970976

971977
it("should warn when server does not exist", async () => {
972-
global.fetch = createFetchMock({
973-
[API_PATHS.HEALTH_ALL]: {
974-
servers: [
975-
{ name: "server1", status: "ok" },
976-
// 'nonexistent' not in health response.
977-
],
978-
},
979-
[API_PATHS.SERVER_TOOLS("server1")]: {
980-
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
981-
},
982-
});
978+
vi.stubGlobal(
979+
"fetch",
980+
createFetchMock({
981+
[API_PATHS.HEALTH_ALL]: {
982+
servers: [
983+
{ name: "server1", status: "ok" },
984+
// 'nonexistent' not in health response.
985+
],
986+
},
987+
[API_PATHS.SERVER_TOOLS("server1")]: {
988+
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
989+
},
990+
}),
991+
);
983992

984993
const customWarn = vi.fn();
985994
const loggerClient = new McpdClient({
@@ -995,21 +1004,24 @@ describe("McpdClient", () => {
9951004
});
9961005

9971006
it("should not warn when all servers are healthy", async () => {
998-
global.fetch = createFetchMock({
999-
[API_PATHS.SERVERS]: ["server1", "server2"],
1000-
[API_PATHS.HEALTH_ALL]: {
1001-
servers: [
1002-
{ name: "server1", status: "ok" },
1003-
{ name: "server2", status: "ok" },
1004-
],
1005-
},
1006-
[API_PATHS.SERVER_TOOLS("server1")]: {
1007-
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
1008-
},
1009-
[API_PATHS.SERVER_TOOLS("server2")]: {
1010-
tools: [{ name: "tool2", description: "Tool 2", inputSchema: {} }],
1011-
},
1012-
});
1007+
vi.stubGlobal(
1008+
"fetch",
1009+
createFetchMock({
1010+
[API_PATHS.SERVERS]: ["server1", "server2"],
1011+
[API_PATHS.HEALTH_ALL]: {
1012+
servers: [
1013+
{ name: "server1", status: "ok" },
1014+
{ name: "server2", status: "ok" },
1015+
],
1016+
},
1017+
[API_PATHS.SERVER_TOOLS("server1")]: {
1018+
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
1019+
},
1020+
[API_PATHS.SERVER_TOOLS("server2")]: {
1021+
tools: [{ name: "tool2", description: "Tool 2", inputSchema: {} }],
1022+
},
1023+
}),
1024+
);
10131025

10141026
const customWarn = vi.fn();
10151027
const loggerClient = new McpdClient({
@@ -1023,23 +1035,26 @@ describe("McpdClient", () => {
10231035
});
10241036

10251037
it("should log multiple warnings correctly", async () => {
1026-
global.fetch = createFetchMock({
1027-
[API_PATHS.SERVERS]: [
1028-
"healthy",
1029-
"timeout_server",
1030-
"unreachable_server",
1031-
],
1032-
[API_PATHS.HEALTH_ALL]: {
1033-
servers: [
1034-
{ name: "healthy", status: "ok" },
1035-
{ name: "timeout_server", status: "timeout" },
1036-
{ name: "unreachable_server", status: "unreachable" },
1038+
vi.stubGlobal(
1039+
"fetch",
1040+
createFetchMock({
1041+
[API_PATHS.SERVERS]: [
1042+
"healthy",
1043+
"timeout_server",
1044+
"unreachable_server",
10371045
],
1038-
},
1039-
[API_PATHS.SERVER_TOOLS("healthy")]: {
1040-
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
1041-
},
1042-
});
1046+
[API_PATHS.HEALTH_ALL]: {
1047+
servers: [
1048+
{ name: "healthy", status: "ok" },
1049+
{ name: "timeout_server", status: "timeout" },
1050+
{ name: "unreachable_server", status: "unreachable" },
1051+
],
1052+
},
1053+
[API_PATHS.SERVER_TOOLS("healthy")]: {
1054+
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
1055+
},
1056+
}),
1057+
);
10431058

10441059
const customWarn = vi.fn();
10451060
const loggerClient = new McpdClient({
@@ -1059,18 +1074,21 @@ describe("McpdClient", () => {
10591074
});
10601075

10611076
it("should use custom logger when provided", async () => {
1062-
global.fetch = createFetchMock({
1063-
[API_PATHS.SERVERS]: ["healthy", "unhealthy"],
1064-
[API_PATHS.HEALTH_ALL]: {
1065-
servers: [
1066-
{ name: "healthy", status: "ok" },
1067-
{ name: "unhealthy", status: "timeout" },
1068-
],
1069-
},
1070-
[API_PATHS.SERVER_TOOLS("healthy")]: {
1071-
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
1072-
},
1073-
});
1077+
vi.stubGlobal(
1078+
"fetch",
1079+
createFetchMock({
1080+
[API_PATHS.SERVERS]: ["healthy", "unhealthy"],
1081+
[API_PATHS.HEALTH_ALL]: {
1082+
servers: [
1083+
{ name: "healthy", status: "ok" },
1084+
{ name: "unhealthy", status: "timeout" },
1085+
],
1086+
},
1087+
[API_PATHS.SERVER_TOOLS("healthy")]: {
1088+
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
1089+
},
1090+
}),
1091+
);
10741092

10751093
const customWarn = vi.fn();
10761094
const customLogger = {
@@ -1091,18 +1109,21 @@ describe("McpdClient", () => {
10911109
});
10921110

10931111
it("should support partial logger implementation", async () => {
1094-
global.fetch = createFetchMock({
1095-
[API_PATHS.SERVERS]: ["healthy", "unhealthy"],
1096-
[API_PATHS.HEALTH_ALL]: {
1097-
servers: [
1098-
{ name: "healthy", status: "ok" },
1099-
{ name: "unhealthy", status: "timeout" },
1100-
],
1101-
},
1102-
[API_PATHS.SERVER_TOOLS("healthy")]: {
1103-
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
1104-
},
1105-
});
1112+
vi.stubGlobal(
1113+
"fetch",
1114+
createFetchMock({
1115+
[API_PATHS.SERVERS]: ["healthy", "unhealthy"],
1116+
[API_PATHS.HEALTH_ALL]: {
1117+
servers: [
1118+
{ name: "healthy", status: "ok" },
1119+
{ name: "unhealthy", status: "timeout" },
1120+
],
1121+
},
1122+
[API_PATHS.SERVER_TOOLS("healthy")]: {
1123+
tools: [{ name: "tool1", description: "Tool 1", inputSchema: {} }],
1124+
},
1125+
}),
1126+
);
11061127

11071128
const customWarn = vi.fn();
11081129

0 commit comments

Comments
 (0)