Skip to content

Tech debt: Replace Promise.all mocking with counter pattern in rollback tests #125

@2witstudios

Description

@2witstudios

Context

From CodeRabbit review of fix/rollback-activity-feed-issues PR.

Issue

File: apps/web/src/services/api/__tests__/rollback-service.test.ts
Lines: 914-918, 941-944, 1003-1006, 1028-1031

Spying on Promise.all globally is fragile and can interfere with other concurrent tests.

Suggested Fix

Use counter pattern with db.select mock instead:

// Instead of:
vi.spyOn(Promise, 'all').mockResolvedValueOnce([mockActivities, [{ value: 2 }]]);

// Use:
let selectCallCount = 0;
(db.select as Mock).mockImplementation(() => ({
  from: vi.fn().mockReturnValue({
    where: vi.fn().mockReturnValue({
      orderBy: vi.fn().mockReturnValue({
        limit: vi.fn().mockReturnValue({
          offset: vi.fn().mockImplementation(() => {
            selectCallCount++;
            if (selectCallCount === 1) return Promise.resolve(mockActivities);
            return Promise.resolve([{ value: 2 }]);
          }),
        }),
      }),
    }),
  }),
}));

Priority

Low - improvement, not a bug

Effort

Medium

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions