Skip to content

Commit d6e09b7

Browse files
committed
fix(tests): fixing tests
1 parent 30328b3 commit d6e09b7

2 files changed

Lines changed: 7 additions & 47 deletions

File tree

jest.config.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ module.exports = {
4848
clearMocks: true,
4949
restoreMocks: true,
5050

51-
// Force Jest to exit after tests complete
52-
// This prevents hanging caused by lingering HTTP connections (TLSWRAP handles)
53-
forceExit: true,
51+
// Detect open handles to identify resource leaks
52+
detectOpenHandles: true,
5453
};

tests/__utils__/mocks.ts

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,6 @@ function isUpdatingSnapshots(): boolean {
6262
process.argv.includes("-u") ||
6363
process.env.DEEPGRAM_FORCE_REAL_API === "true";
6464

65-
if (isUpdateMode) {
66-
console.log(
67-
"🔍 Detected snapshot update mode - arguments:",
68-
process.argv.filter((arg) => arg.includes("update") || arg === "-u")
69-
);
70-
}
71-
7265
return isUpdateMode;
7366
}
7467

@@ -81,13 +74,9 @@ function createMockFetch(): typeof fetch {
8174
typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
8275
const method = init?.method || "GET";
8376

84-
console.log("🎭 Mock fetch intercepted:", method, url);
85-
8677
// Mock Deepgram API endpoints
8778
if (url.includes("api.deepgram.com")) {
8879
if (url.includes("/v1/listen") && method === "POST") {
89-
console.log("🎭 -> Mocking transcription request");
90-
9180
// Check if it's a callback request (async)
9281
const isCallbackRequest = url.includes("callback=");
9382

@@ -118,17 +107,13 @@ function createMockFetch(): typeof fetch {
118107
}
119108

120109
if (url.includes("/v1/speak") && method === "POST") {
121-
console.log("🎭 -> Mocking TTS request");
122-
123110
return new Response(mockAudioBuffer, {
124111
status: 200,
125112
headers: new Headers(mockTTSHeaders),
126113
});
127114
}
128115

129116
if (url.includes("/v1/models") && method === "GET") {
130-
console.log("🎭 -> Mocking models request");
131-
132117
// Check if it's a specific model request (has model ID in path)
133118
const modelIdMatch = url.match(/\/v1\/models\/([^?]+)/);
134119
const responseData = modelIdMatch ? mockGetModelResponse : mockGetAllModelsResponse;
@@ -140,17 +125,13 @@ function createMockFetch(): typeof fetch {
140125
}
141126

142127
if (url.includes("/v1/auth/grant") && method === "POST") {
143-
console.log("🎭 -> Mocking auth grant token request");
144-
145128
return new Response(JSON.stringify(mockGrantTokenResponse), {
146129
status: 200,
147130
headers: { "content-type": "application/json" },
148131
});
149132
}
150133

151134
if (url.includes("/v1/read") && method === "POST") {
152-
console.log("🎭 -> Mocking read/analyze request");
153-
154135
// Check if it's a callback request (async)
155136
const isCallbackRequest = url.includes("callback=");
156137

@@ -197,8 +178,6 @@ function createMockFetch(): typeof fetch {
197178
}
198179

199180
if (url.includes("/onprem/distribution/credentials")) {
200-
console.log("🎭 -> Mocking self-hosted credentials request");
201-
202181
if (method === "GET") {
203182
// Check if it's a specific credential request (has credential ID in path)
204183
const credentialIdMatch = url.match(/\/credentials\/([^?]+)$/);
@@ -228,16 +207,13 @@ function createMockFetch(): typeof fetch {
228207
}
229208

230209
if (url.includes("/v1/auth/token") && method === "GET") {
231-
console.log("🎭 -> Mocking auth token details request");
232210
return new Response(JSON.stringify(mockGetTokenDetailsResponse), {
233211
status: 200,
234212
headers: { "content-type": "application/json" },
235213
});
236214
}
237215

238216
if (url.includes("/v1/projects") && method === "GET") {
239-
console.log("🎭 -> Mocking manage projects request");
240-
241217
// Check for project keys endpoint
242218
if (url.includes("/keys")) {
243219
// Check if it's a specific key request (has key ID in path after /keys/)
@@ -342,8 +318,6 @@ function createMockFetch(): typeof fetch {
342318
}
343319

344320
if (url.includes("/v1/projects") && method === "POST") {
345-
console.log("🎭 -> Mocking manage projects POST request");
346-
347321
// Check for project key creation
348322
if (url.includes("/keys")) {
349323
return new Response(JSON.stringify(mockCreateProjectKeyResponse), {
@@ -362,8 +336,6 @@ function createMockFetch(): typeof fetch {
362336
}
363337

364338
if (url.includes("/v1/projects") && method === "PATCH") {
365-
console.log("🎭 -> Mocking manage projects PATCH request");
366-
367339
// Check for project updates
368340
if (url.match(/\/v1\/projects\/[^/]+$/)) {
369341
return new Response(JSON.stringify(mockUpdateProjectResponse), {
@@ -374,8 +346,6 @@ function createMockFetch(): typeof fetch {
374346
}
375347

376348
if (url.includes("/v1/projects") && method === "PUT") {
377-
console.log("🎭 -> Mocking manage projects PUT request");
378-
379349
// Check for member scope updates
380350
if (url.includes("/scopes")) {
381351
return new Response(JSON.stringify(mockUpdateProjectMemberScopeResponse), {
@@ -386,8 +356,6 @@ function createMockFetch(): typeof fetch {
386356
}
387357

388358
if (url.includes("/v1/projects") && method === "DELETE") {
389-
console.log("🎭 -> Mocking manage projects DELETE request");
390-
391359
// Check for leave project (special case - returns JSON response)
392360
if (url.includes("/leave")) {
393361
return new Response(JSON.stringify(mockLeaveProjectResponse), {
@@ -431,24 +399,21 @@ function createMockFetch(): typeof fetch {
431399
}
432400

433401
// If we get here, it's not a Deepgram API request we're mocking
434-
console.log("🎭 -> Unmocked request, throwing error to simulate offline");
435402
throw new Error(`Network request blocked in offline test mode: ${method} ${url}`);
436403
};
437404
}
438405

439406
/**
440-
* Sets up API mocks for e2e tests when not updating snapshots
407+
* Sets up API mocks for e2e tests
441408
* This allows tests to run without internet/API key requirements
409+
* E2E tests use mocks by default, but can be toggled to real APIs
442410
*/
443411
export function setupApiMocks(): void {
444-
// Only mock if we're NOT updating snapshots
445-
if (isUpdatingSnapshots()) {
446-
console.log("📸 Snapshot update mode: Using real API calls");
447-
return;
412+
// Check if we should use real APIs instead of mocks
413+
if (process.env.DEEPGRAM_FORCE_REAL_API === "true") {
414+
return; // Skip mock setup - use real APIs
448415
}
449416

450-
console.log("🎭 Mock mode: Using custom fetch mocks");
451-
452417
// Store original implementations
453418
originalFetch = global.fetch;
454419

@@ -477,8 +442,6 @@ export function setupApiMocks(): void {
477442
} catch (_e) {
478443
// Might not be available
479444
}
480-
481-
console.log("🎭 Custom fetch mocks setup complete");
482445
}
483446

484447
/**
@@ -505,6 +468,4 @@ export function cleanupApiMocks(): void {
505468
// Might not be available
506469
}
507470
}
508-
509-
console.log("🎭 Custom fetch mocks cleaned up");
510471
}

0 commit comments

Comments
 (0)