fix: correct test setup and cleanup in group-sync tests

main
borja (aider) 3 months ago
parent 86de177b94
commit 936a864a9e

@ -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 { GroupSyncService } from '../../../src/services/group-sync';
import { db } from '../../../src/db'; import { db } from '../../../src/db';
@ -30,11 +30,20 @@ describe('GroupSyncService', () => {
beforeEach(() => { beforeEach(() => {
db.exec('DELETE FROM groups'); db.exec('DELETE FROM groups');
GroupSyncService['lastSyncAttempt'] = 0; 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(() => { 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; globalThis.fetch = originalFetch;
}); });
@ -88,7 +97,10 @@ describe('GroupSyncService', () => {
}); });
it('should handle API errors', async () => { 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'); await expect(GroupSyncService.syncGroups()).rejects.toThrow('API request failed: Not Found');
}); });
}); });

Loading…
Cancel
Save