-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
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
StaticMockLinkinstance 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:
- PR: fix: add missing isRecurringEventTemplate field to event mock #6962
- Review comment: fix: add missing isRecurringEventTemplate field to event mock #6962 (comment)
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
StaticMockLinkor similar Apollo mock objects outside test functions - Refactor to move instantiation into
beforeEachhooks for proper test isolation - Verify all affected tests still pass after refactoring
- Document the pattern in testing guidelines to prevent regression
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Status