From 936a864a9e73ebb5e35d5e7531b4c10e9866e99f Mon Sep 17 00:00:00 2001 From: "borja (aider)" Date: Sat, 29 Mar 2025 22:25:07 +0100 Subject: [PATCH] fix: correct test setup and cleanup in group-sync tests --- tests/unit/services/group-sync.test.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/unit/services/group-sync.test.ts b/tests/unit/services/group-sync.test.ts index f25410b..fa38bf3 100644 --- a/tests/unit/services/group-sync.test.ts +++ b/tests/unit/services/group-sync.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, beforeEach, mock, spyOn } from 'bun:test'; +import { describe, it, expect, beforeEach, afterAll, mock } from 'bun:test'; import { GroupSyncService } from '../../../src/services/group-sync'; import { db } from '../../../src/db'; @@ -30,11 +30,20 @@ describe('GroupSyncService', () => { beforeEach(() => { db.exec('DELETE FROM groups'); GroupSyncService['lastSyncAttempt'] = 0; - Object.assign(env, mockEnv); + // Reset process.env to mock values + process.env.WHATSAPP_COMMUNITY_ID = 'test-community'; + process.env.EVOLUTION_API_URL = 'http://test-api'; + process.env.EVOLUTION_API_INSTANCE = 'test-instance'; + process.env.EVOLUTION_API_KEY = 'test-key'; }); afterAll(() => { - process.env = originalEnv; + // Restore original process.env + process.env.WHATSAPP_COMMUNITY_ID = originalEnv.WHATSAPP_COMMUNITY_ID; + process.env.EVOLUTION_API_URL = originalEnv.EVOLUTION_API_URL; + process.env.EVOLUTION_API_INSTANCE = originalEnv.EVOLUTION_API_INSTANCE; + process.env.EVOLUTION_API_KEY = originalEnv.EVOLUTION_API_KEY; + // Restore original fetch globalThis.fetch = originalFetch; }); @@ -88,7 +97,10 @@ describe('GroupSyncService', () => { }); it('should handle API errors', async () => { - mockFetch.mockImplementationOnce(() => Promise.resolve({ ok: false, statusText: 'Not Found' })); + globalThis.fetch = mock(async () => ({ + ok: false, + statusText: 'Not Found' + })); await expect(GroupSyncService.syncGroups()).rejects.toThrow('API request failed: Not Found'); }); });