fix: correct fetch mocking in group-sync tests

main
borja (aider) 3 months ago
parent 7cef5448c4
commit 86de177b94

@ -2,8 +2,9 @@ import { describe, it, expect, beforeEach, mock, spyOn } 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';
// Mock process.env for testing // Store original globals
const originalEnv = { ...process.env }; const originalEnv = { ...process.env };
const originalFetch = globalThis.fetch;
process.env = { process.env = {
WHATSAPP_COMMUNITY_ID: 'test-community', WHATSAPP_COMMUNITY_ID: 'test-community',
EVOLUTION_API_URL: 'http://test-api', EVOLUTION_API_URL: 'http://test-api',
@ -13,7 +14,7 @@ process.env = {
}; };
// Mock fetch // Mock fetch
const mockFetch = mock.global('fetch', async () => ({ const fetchMock = mock(async () => ({
ok: true, ok: true,
json: async () => [ json: async () => [
{ id: 'group1', subject: 'Group 1', linkedParent: 'test-community' }, { 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', () => { describe('GroupSyncService', () => {
beforeEach(() => { beforeEach(() => {
db.exec('DELETE FROM groups'); db.exec('DELETE FROM groups');
@ -31,7 +35,7 @@ describe('GroupSyncService', () => {
afterAll(() => { afterAll(() => {
process.env = originalEnv; process.env = originalEnv;
mockFetch.mockRestore(); globalThis.fetch = originalFetch;
}); });
describe('syncGroups', () => { describe('syncGroups', () => {

Loading…
Cancel
Save