feat: add retryDelay, retryCondition, and retryStrategy options for enhanced test retry control (fix #8482)#8812
Closed
matanshavit wants to merge 6 commits intovitest-dev:mainfrom
Closed
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements enhanced retry options for Vitest tests, addressing feature request #8482. The current
retrymechanism only retries a fixed number of times on any kind of error. This enhancement introduces a nested retry configuration object that provides greater flexibility and control over test retry behavior while maintaining full backward compatibility.New Features
The
retryoption now accepts either a number (for backward compatibility) or an object with the following properties:count- Number of retry attemptsnumber0retrynumber optiondelay- Delay between retry attemptsnumber(milliseconds)0condition- Conditional retry based on error typestring | ((error: Error) => boolean)undefined(retry on all errors)trueto retrystrategy- When to retry failed tests'immediate' | 'test-file' | 'deferred''immediate'immediate: Retry immediately after failure (current behavior, default)test-file: Defer retries until after all tests in the current file completedeferred: Defer retries until after all test files completeUsage Examples
Configuration
All options can be configured globally in
vitest.config.ts:Important Notes
Function Conditions in Config
retry.conditioncannot be used invitest.config.tsbecause configurations are serialized when passed to worker threads. If you need conditional retry logic, you have two options:Use a string pattern in your config (works everywhere):
Use a function directly in test files (test-level only):
Implementation Details
Core Changes
shouldRetryTest(): Helper function to evaluate retry conditionscollectDeferredRetryTests(): Collects tests with deferred retry strategiesrunTest(): Handles immediate strategy with delay and conditionrunSuite(): Handles test-file strategy retriesrunFiles(): Handles deferred strategy retriesType Definitions
TaskBaseandTestOptionsto support nested retry structureVitestRunnerConfigto support nested retry structureInlineConfigwith proper documentation and serialization warningsConfiguration Support
Test Coverage
retry.condition(string regex and function)retry.delaytiming validationBackward Compatibility
✅ Fully backward compatible - The existing
retry: numbersyntax continues to work exactly as before:retry: 3is equivalent toretry: { count: 3 }delaydefaults to0(no delay)conditiondefaults toundefined(retry on all errors)strategydefaults to'immediate'(retry immediately)Existing tests with the
retryoption will continue to work exactly as before with no changes required.Test Plan
Related Issues
Closes #8482
Related (future enhancement): #7834
Comparison with Other Test Runners
This implementation provides similar functionality to:
retryTimes(numRetries, { waitBeforeRetry: 100 })→ Ourretry.delayspecFileRetriesDelay→ Ourretry.delayspecFileRetriesDeferred→ Ourretry.strategy: 'deferred'Our implementation goes beyond existing solutions by:
retry.condition