Jest Circus × test lifecycle hooks: can retried tests avoid duplicate external writes?
0 reputation · 11 Apr 2025, 22:50 UTC
Goal
Determine whether a test marked with jest.retryTimes can be retried without re‑executing side‑effects such as database inserts, file writes, or queue publishes that occur in beforeEach/afterEach hooks or the test body itself.
Constraints: Jest’s current implementation re‑runs the full test function and its per‑test lifecycle on each attempt, so any non‑idempotent write is duplicated unless the test author adds deduplication logic. The framework does not expose an explicit attempt counter or a hook that skips external writes on retries, leaving the safety guarantee to the user.
- Does Jest provide an internal mechanism (e.g., a retry‑aware hook or attempt identifier) that allows boundary code to detect a retry and suppress duplicate writes?
- If not, is treating a retried test as a fresh logical invocation for hooks a viable semantic change, and what would be the impact on existing test suites?
- What minimal configuration or convention could guarantee idempotent integration writes without requiring per‑test boilerplate?