Skip to content

Commit 8c84cc7

Browse files
committed
Fix tests to not have unhandled console.errors - use assertions when console errors are expected
1 parent 2be4820 commit 8c84cc7

7 files changed

Lines changed: 196 additions & 0 deletions

File tree

src/tools/sandbox/__tests__/getMessages.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,28 @@ describe("getMessages", () => {
9393
});
9494

9595
it("should handle missing MAILTRAP_TEST_INBOX_ID", async () => {
96+
const consoleErrorSpy = jest
97+
.spyOn(console, "error")
98+
.mockImplementation(() => {});
9699
delete process.env.MAILTRAP_TEST_INBOX_ID;
97100

98101
const result = await getMessages({});
99102

103+
expect(consoleErrorSpy).toHaveBeenCalledWith(
104+
"Error getting messages:",
105+
expect.anything()
106+
);
100107
expect(result.isError).toBe(true);
101108
expect(result.content[0].text).toContain(
102109
"MAILTRAP_TEST_INBOX_ID environment variable is required"
103110
);
111+
consoleErrorSpy.mockRestore();
104112
});
105113

106114
it("should handle missing sandbox client", async () => {
115+
const consoleErrorSpy = jest
116+
.spyOn(console, "error")
117+
.mockImplementation(() => {});
107118
// Mock sandboxClient as null for this test
108119
jest.doMock("../../../client", () => ({
109120
sandboxClient: null,
@@ -118,19 +129,32 @@ describe("getMessages", () => {
118129
jest.dontMock("../../../client");
119130
jest.resetModules();
120131

132+
expect(consoleErrorSpy).toHaveBeenCalledWith(
133+
"Error getting messages:",
134+
expect.anything()
135+
);
121136
expect(result.isError).toBe(true);
122137
expect(result.content[0].text).toContain("Sandbox client is not available");
138+
consoleErrorSpy.mockRestore();
123139
});
124140

125141
it("should handle API errors", async () => {
126142
const mockError = new Error("API Error");
127143
(sandboxClient as any).testing.messages.get.mockRejectedValue(mockError);
144+
const consoleErrorSpy = jest
145+
.spyOn(console, "error")
146+
.mockImplementation(() => {});
128147

129148
const result = await getMessages({});
130149

150+
expect(consoleErrorSpy).toHaveBeenCalledWith(
151+
"Error getting messages:",
152+
mockError
153+
);
131154
expect(result.isError).toBe(true);
132155
expect(result.content[0].text).toContain("Failed to get messages");
133156
expect(result.content[0].text).toContain("API Error");
157+
consoleErrorSpy.mockRestore();
134158
});
135159

136160
it("should pass page parameter to SDK", async () => {

src/tools/sandbox/__tests__/sendSandboxEmail.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,27 @@ describe("sendSandboxEmail", () => {
314314
});
315315

316316
describe("errors handling", () => {
317+
let consoleErrorSpy: jest.SpyInstance;
318+
319+
beforeEach(() => {
320+
consoleErrorSpy = jest
321+
.spyOn(console, "error")
322+
.mockImplementation(() => {});
323+
});
324+
325+
afterEach(() => {
326+
consoleErrorSpy.mockRestore();
327+
});
328+
317329
it("should throw error when MAILTRAP_TEST_INBOX_ID is not set", async () => {
318330
delete process.env.MAILTRAP_TEST_INBOX_ID;
319331

320332
const result = await sendSandboxEmail(mockEmailData);
321333

334+
expect(consoleErrorSpy).toHaveBeenCalledWith(
335+
"Error sending sandbox email:",
336+
expect.anything()
337+
);
322338
expect((sandboxClient as any).send).not.toHaveBeenCalled();
323339
expect(result).toEqual({
324340
content: [
@@ -338,6 +354,10 @@ describe("sendSandboxEmail", () => {
338354
subject: mockEmailData.subject,
339355
});
340356

357+
expect(consoleErrorSpy).toHaveBeenCalledWith(
358+
"Error sending sandbox email:",
359+
expect.anything()
360+
);
341361
expect((sandboxClient as any).send).not.toHaveBeenCalled();
342362
expect(result).toEqual({
343363
content: [
@@ -356,6 +376,10 @@ describe("sendSandboxEmail", () => {
356376

357377
const result = await sendSandboxEmail(mockEmailData);
358378

379+
expect(consoleErrorSpy).toHaveBeenCalledWith(
380+
"Error sending sandbox email:",
381+
mockError
382+
);
359383
expect(result).toEqual({
360384
content: [
361385
{

src/tools/sandbox/__tests__/showEmailMessage.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,49 @@ describe("showEmailMessage", () => {
7474
});
7575

7676
it("should handle message without HTML content", async () => {
77+
const consoleWarnSpy = jest
78+
.spyOn(console, "warn")
79+
.mockImplementation(() => {});
7780
(sandboxClient as any).testing.messages.getHtmlMessage.mockRejectedValue(
7881
new Error("No HTML")
7982
);
8083

8184
const result = await showEmailMessage({ message_id: 1 });
8285

86+
expect(consoleWarnSpy).toHaveBeenCalledWith(
87+
"Could not retrieve HTML content:",
88+
expect.any(Error)
89+
);
8390
expect(result.content[0].text).toContain("Sandbox Email Message Details");
8491
expect(result.content[0].text).toContain("Text Content");
8592
expect(result.content[0].text).not.toContain("HTML Content");
93+
consoleWarnSpy.mockRestore();
8694
});
8795

8896
it("should handle message without text content", async () => {
97+
const consoleWarnSpy = jest
98+
.spyOn(console, "warn")
99+
.mockImplementation(() => {});
89100
(sandboxClient as any).testing.messages.getTextMessage.mockRejectedValue(
90101
new Error("No text")
91102
);
92103

93104
const result = await showEmailMessage({ message_id: 1 });
94105

106+
expect(consoleWarnSpy).toHaveBeenCalledWith(
107+
"Could not retrieve text content:",
108+
expect.any(Error)
109+
);
95110
expect(result.content[0].text).toContain("Sandbox Email Message Details");
96111
expect(result.content[0].text).toContain("HTML Content");
97112
expect(result.content[0].text).not.toContain("Text Content");
113+
consoleWarnSpy.mockRestore();
98114
});
99115

100116
it("should handle message without both HTML and text", async () => {
117+
const consoleWarnSpy = jest
118+
.spyOn(console, "warn")
119+
.mockImplementation(() => {});
101120
(sandboxClient as any).testing.messages.getHtmlMessage.mockRejectedValue(
102121
new Error("No HTML")
103122
);
@@ -107,10 +126,19 @@ describe("showEmailMessage", () => {
107126

108127
const result = await showEmailMessage({ message_id: 1 });
109128

129+
expect(consoleWarnSpy).toHaveBeenCalledWith(
130+
"Could not retrieve HTML content:",
131+
expect.any(Error)
132+
);
133+
expect(consoleWarnSpy).toHaveBeenCalledWith(
134+
"Could not retrieve text content:",
135+
expect.any(Error)
136+
);
110137
expect(result.content[0].text).toContain("Sandbox Email Message Details");
111138
expect(result.content[0].text).toContain(
112139
"Message body content could not be retrieved"
113140
);
141+
consoleWarnSpy.mockRestore();
114142
});
115143

116144
it("should handle null message response", async () => {
@@ -125,17 +153,28 @@ describe("showEmailMessage", () => {
125153
});
126154

127155
it("should handle missing MAILTRAP_TEST_INBOX_ID", async () => {
156+
const consoleErrorSpy = jest
157+
.spyOn(console, "error")
158+
.mockImplementation(() => {});
128159
delete process.env.MAILTRAP_TEST_INBOX_ID;
129160

130161
const result = await showEmailMessage({ message_id: 1 });
131162

163+
expect(consoleErrorSpy).toHaveBeenCalledWith(
164+
"Error showing sandbox email message:",
165+
expect.anything()
166+
);
132167
expect(result.isError).toBe(true);
133168
expect(result.content[0].text).toContain(
134169
"MAILTRAP_TEST_INBOX_ID environment variable is required"
135170
);
171+
consoleErrorSpy.mockRestore();
136172
});
137173

138174
it("should handle missing sandbox client", async () => {
175+
const consoleErrorSpy = jest
176+
.spyOn(console, "error")
177+
.mockImplementation(() => {});
139178
// Mock sandboxClient as null for this test
140179
jest.doMock("../../../client", () => ({
141180
sandboxClient: null,
@@ -151,22 +190,35 @@ describe("showEmailMessage", () => {
151190
jest.dontMock("../../../client");
152191
jest.resetModules();
153192

193+
expect(consoleErrorSpy).toHaveBeenCalledWith(
194+
"Error showing sandbox email message:",
195+
expect.anything()
196+
);
154197
expect(result.isError).toBe(true);
155198
expect(result.content[0].text).toContain("Sandbox client is not available");
199+
consoleErrorSpy.mockRestore();
156200
});
157201

158202
it("should handle API errors", async () => {
159203
const mockError = new Error("API Error");
160204
(sandboxClient as any).testing.messages.showEmailMessage.mockRejectedValue(
161205
mockError
162206
);
207+
const consoleErrorSpy = jest
208+
.spyOn(console, "error")
209+
.mockImplementation(() => {});
163210

164211
const result = await showEmailMessage({ message_id: 1 });
165212

213+
expect(consoleErrorSpy).toHaveBeenCalledWith(
214+
"Error showing sandbox email message:",
215+
mockError
216+
);
166217
expect(result.isError).toBe(true);
167218
expect(result.content[0].text).toContain(
168219
"Failed to show sandbox email message"
169220
);
170221
expect(result.content[0].text).toContain("API Error");
222+
consoleErrorSpy.mockRestore();
171223
});
172224
});

src/tools/templates/__tests__/createTemplate.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,28 @@ describe("createTemplate", () => {
136136
});
137137

138138
describe("error handling", () => {
139+
let consoleErrorSpy: jest.SpyInstance;
140+
141+
beforeEach(() => {
142+
consoleErrorSpy = jest
143+
.spyOn(console, "error")
144+
.mockImplementation(() => {});
145+
});
146+
147+
afterEach(() => {
148+
consoleErrorSpy.mockRestore();
149+
});
150+
139151
it("should handle client.templates.create failure", async () => {
140152
const mockError = new Error("Failed to create template");
141153
(client.templates.create as jest.Mock).mockRejectedValue(mockError);
142154

143155
const result = await createTemplate(mockTemplateData);
144156

157+
expect(consoleErrorSpy).toHaveBeenCalledWith(
158+
"Error creating template:",
159+
mockError
160+
);
145161
expect(result).toEqual({
146162
content: [
147163
{
@@ -159,6 +175,10 @@ describe("createTemplate", () => {
159175

160176
const result = await createTemplate(mockTemplateData);
161177

178+
expect(consoleErrorSpy).toHaveBeenCalledWith(
179+
"Error creating template:",
180+
mockError
181+
);
162182
expect(result).toEqual({
163183
content: [
164184
{

src/tools/templates/__tests__/deleteTemplate.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,28 @@ describe("deleteTemplate", () => {
4949
});
5050

5151
describe("error handling", () => {
52+
let consoleErrorSpy: jest.SpyInstance;
53+
54+
beforeEach(() => {
55+
consoleErrorSpy = jest
56+
.spyOn(console, "error")
57+
.mockImplementation(() => {});
58+
});
59+
60+
afterEach(() => {
61+
consoleErrorSpy.mockRestore();
62+
});
63+
5264
it("should handle client.templates.delete failure", async () => {
5365
const mockError = new Error("Failed to delete template");
5466
(client.templates.delete as jest.Mock).mockRejectedValue(mockError);
5567

5668
const result = await deleteTemplate({ template_id: mockTemplateId });
5769

70+
expect(consoleErrorSpy).toHaveBeenCalledWith(
71+
"Error deleting template:",
72+
mockError
73+
);
5874
expect(result).toEqual({
5975
content: [
6076
{
@@ -72,6 +88,10 @@ describe("deleteTemplate", () => {
7288

7389
const result = await deleteTemplate({ template_id: mockTemplateId });
7490

91+
expect(consoleErrorSpy).toHaveBeenCalledWith(
92+
"Error deleting template:",
93+
mockError
94+
);
7595
expect(result).toEqual({
7696
content: [
7797
{
@@ -89,6 +109,10 @@ describe("deleteTemplate", () => {
89109

90110
const result = await deleteTemplate({ template_id: mockTemplateId });
91111

112+
expect(consoleErrorSpy).toHaveBeenCalledWith(
113+
"Error deleting template:",
114+
mockError
115+
);
92116
expect(result).toEqual({
93117
content: [
94118
{

src/tools/templates/__tests__/listTemplates.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,28 @@ describe("listTemplates", () => {
154154
});
155155

156156
describe("error handling", () => {
157+
let consoleErrorSpy: jest.SpyInstance;
158+
159+
beforeEach(() => {
160+
consoleErrorSpy = jest
161+
.spyOn(console, "error")
162+
.mockImplementation(() => {});
163+
});
164+
165+
afterEach(() => {
166+
consoleErrorSpy.mockRestore();
167+
});
168+
157169
it("should handle client.templates.getList failure", async () => {
158170
const mockError = new Error("Failed to fetch templates");
159171
(client.templates.getList as jest.Mock).mockRejectedValue(mockError);
160172

161173
const result = await listTemplates();
162174

175+
expect(consoleErrorSpy).toHaveBeenCalledWith(
176+
"Error listing templates:",
177+
mockError
178+
);
163179
expect(result).toEqual({
164180
content: [
165181
{
@@ -177,6 +193,10 @@ describe("listTemplates", () => {
177193

178194
const result = await listTemplates();
179195

196+
expect(consoleErrorSpy).toHaveBeenCalledWith(
197+
"Error listing templates:",
198+
mockError
199+
);
180200
expect(result).toEqual({
181201
content: [
182202
{
@@ -194,6 +214,10 @@ describe("listTemplates", () => {
194214

195215
const result = await listTemplates();
196216

217+
expect(consoleErrorSpy).toHaveBeenCalledWith(
218+
"Error listing templates:",
219+
mockError
220+
);
197221
expect(result).toEqual({
198222
content: [
199223
{

0 commit comments

Comments
 (0)