From 86de177b946735c60bd41045eaa81b4e455e387e Mon Sep 17 00:00:00 2001 From: "borja (aider)" Date: Sat, 29 Mar 2025 22:24:20 +0100 Subject: [PATCH] fix: correct fetch mocking in group-sync tests --- tests/unit/services/group-sync.test.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/unit/services/group-sync.test.ts b/tests/unit/services/group-sync.test.ts index be50da7..f25410b 100644 --- a/tests/unit/services/group-sync.test.ts +++ b/tests/unit/services/group-sync.test.ts @@ -2,8 +2,9 @@ import { describe, it, expect, beforeEach, mock, spyOn } from 'bun:test'; import { GroupSyncService } from '../../../src/services/group-sync'; import { db } from '../../../src/db'; -// Mock process.env for testing +// Store original globals const originalEnv = { ...process.env }; +const originalFetch = globalThis.fetch; process.env = { WHATSAPP_COMMUNITY_ID: 'test-community', EVOLUTION_API_URL: 'http://test-api', @@ -13,7 +14,7 @@ process.env = { }; // Mock fetch -const mockFetch = mock.global('fetch', async () => ({ +const fetchMock = mock(async () => ({ ok: true, json: async () => [ { id: 'group1', subject: 'Group 1', linkedParent: 'test-community' }, @@ -22,6 +23,9 @@ const mockFetch = mock.global('fetch', async () => ({ ] })); +// Replace global fetch +globalThis.fetch = fetchMock; + describe('GroupSyncService', () => { beforeEach(() => { db.exec('DELETE FROM groups'); @@ -31,7 +35,7 @@ describe('GroupSyncService', () => { afterAll(() => { process.env = originalEnv; - mockFetch.mockRestore(); + globalThis.fetch = originalFetch; }); describe('syncGroups', () => {