Does jest.retryTimes reset module state between retries, or are side-effect writes duplicated?
0 reputation · 17 Mar 2021, 23:23 UTC
I am designing a test suite that uses jest.retryTimes(2) to tolerate occasional flakiness in integration tests that write to a shared test database and filesystem fixtures.
The documented behavior is clear that a failed test function is re-executed from the start, but it is unclear to me whether Jest resets the module registry or any other environment state between those retry attempts. My concern is that non-idempotent operations inside the test body — inserts, file appends, counter increments — would run once per attempt, silently duplicating writes and corrupting shared fixtures for later tests.
The configuration options resetModules, resetMocks, and clearMocks look relevant, but I cannot find documentation describing how they interact with retries specifically: whether they apply between retry attempts of the same test, or only between separate test files/cases.
So my questions are:
- Does
jest.retryTimesreset module state or mocks between retry attempts by default, or must this be configured explicitly? - Do
resetModules: trueandclearMocks: truetake effect between retries of the same test, and is this behavior stable across recent Jest versions? - Is the recommended pattern to keep non-idempotent writes out of retried test bodies entirely, or is there a supported hook-level cleanup strategy?