Skip to content

Improve test isolation: Move StaticMockLink instantiation to beforeEach hooks #7164

@coderabbitai

Description

@coderabbitai

Problem

Multiple test files instantiate StaticMockLink objects once outside of test functions, creating shared state across all tests in a suite. In sharded test environments where multiple workers run tests in parallel, this pattern can cause:

  • Race conditions between tests running in the same file
  • Cache pollution from previous test executions
  • Unpredictable behavior when tests execute in different orders
  • State retention between test runs in the same worker

Current Pattern (Problematic)

// StaticMockLink created once, shared across all tests
const mockWithTime = new StaticMockLink(MOCKS_WITH_FIXED_TIME, true);

describe('Event Management', () => {
  let user: ReturnType<typeof userEvent.setup>;

  beforeEach(() => {
    user = userEvent.setup();
  });

  const renderEventManagement = (): RenderResult => {
    return render(
      <MockedProvider link={mockWithTime} mocks={MOCKS_WITH_FIXED_TIME}>
        {/* ... */}
      </MockedProvider>,
    );
  };

  it('test 1', () => { /* ... */ });
  it('test 2', () => { /* ... */ });
});

Recommended Pattern (Better Isolation)

describe('Event Management', () => {
  let user: ReturnType<typeof userEvent.setup>;
  let mockWithTime: StaticMockLink; // Declare but don't instantiate

  beforeEach(() => {
    user = userEvent.setup();
    // Create fresh mock link for each test
    mockWithTime = new StaticMockLink(MOCKS_WITH_FIXED_TIME, true);
  });

  const renderEventManagement = (): RenderResult => {
    return render(
      <MockedProvider link={mockWithTime} mocks={MOCKS_WITH_FIXED_TIME}>
        {/* ... */}
      </MockedProvider>,
    );
  };

  it('test 1', () => { /* ... */ });
  it('test 2', () => { /* ... */ });
});

Benefits

  • ✅ Each test gets a fresh StaticMockLink instance with clean state
  • ✅ Eliminates potential race conditions in sharded test environments
  • ✅ Improves test isolation and predictability
  • ✅ Reduces risk of flaky tests in CI/CD pipelines

Affected Files

Based on code review, the following test files use this pattern:

  • src/screens/AdminPortal/EventManagement/EventManagement.spec.tsx (line 63)
  • src/screens/UserPortal/Campaigns/Campaigns.spec.tsx (lines 48-50)
  • src/shared-components/CheckIn/CheckInWrapper.spec.tsx (line 24)
  • src/screens/UserPortal/Campaigns/PledgeModal.spec.tsx (line 284)
  • Possibly others throughout the codebase

Context

This issue was discovered during review of PR #6962, which fixed a missing GraphQL field in mock data. While reviewing test structure for potential flakiness in sharded environments, this pattern was identified as a potential source of non-deterministic test failures.

Reference:

Priority

🟡 Medium - Not causing immediate failures, but represents a theoretical risk in highly parallel test execution environments. This improvement would enhance test reliability and reduce potential CI flakiness.

Acceptance Criteria

  • Identify all test files that instantiate StaticMockLink or similar Apollo mock objects outside test functions
  • Refactor to move instantiation into beforeEach hooks for proper test isolation
  • Verify all affected tests still pass after refactoring
  • Document the pattern in testing guidelines to prevent regression

Metadata

Metadata

Assignees

No one assigned

    Labels

    ci/cdPull requests that update GitHub Actions coderefactorRefactoring tasksunapproved

    Type

    No type

    Projects

    Status

    Backlog

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions