Skip to content

Conversation

naaa760
Copy link

@naaa760 naaa760 commented Sep 7, 2025

Description

This PR implements enhanced retry options for Vitest tests, extending the current simple retry mechanism with advanced configuration capabilities.

fixes: #8482

What This PR Solves

The current Vitest retry mechanism only supports a fixed number of retries on any kind of error. This PR adds:

New Retry Features:

  • retryStrategy: Choose when retries are executed ("immediate", "test-file", "deferred")
  • retryDelay: Configurable delay between retries with exponential backoff
  • retryCondition: Conditional retry based on error type (string patterns, RegExp, or custom functions)

Backward Compatibility:

  • Fully maintains existing retry: number syntax
  • No breaking changes to current API
  • All existing tests continue to work

Implementation Details

Files Modified:

  • Configuration types in packages/vitest/src/node/types/config.ts
  • Core retry logic in packages/runner/src/run.ts
  • CLI support in packages/vitest/src/node/cli/cli-config.ts
  • Utility functions in packages/vitest/src/utils/retry.ts (new file)

Key Features:

  • Exponential backoff delay mechanism
  • Flexible error condition matching
  • TypeScript type safety
  • CLI configuration support
  • Comprehensive test coverage

Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR. Looks good, but needs some polishing.

Please, add this change to the documentation and add some tests that work with this feature

testTimeout: number
hookTimeout: number
retry: number
retry: number | {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the type is repeated often. Maybe it should be a separate type in ./tasks?

catch (e) {
failTask(test.result, e, runner.config.diffOptions)

const error = test.result.errors?.[0]?.error as Error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this line, please? Why do we need to go to test.result.errors?.[0]?.error when e is available already?

}
}

// update retry info
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not remove comments not related to the changes made

}
}

// if test is marked to be failed, flip the result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not remove comments not related to the changes made

description:
'Retry the test specific number of times if it fails (default: `0`)',
argument: '<times>',
'Retry configuration. Can be a number or JSON string with advanced options (default: `0`)',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It cannot be a JSON string, the value would be defined as --retry.count=5 --retry.strategy=immediate

'Stop test execution when given number of tests have failed (default: `0`)',
argument: '<number>',
},
retry: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add a test for CLI input to test/core/cli-test.test.ts

argument: '<milliseconds>',
},
condition: {
description: 'Condition for retrying: RegExp pattern or custom function',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs an explanation where this pattern is applied. According to types, you can also pass down a string, but this message doesn't mention it

* @default 0
*/
retry?: number
retry?: number | {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep comments, please

@@ -0,0 +1,70 @@
import type { SerializedConfig } from '../runtime/config'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole file should be in packages/runner. There is no reason to keep it in another package

export function calculateBackoffDelay(
baseDelay: number,
retryCount: number,
algorithm: 'linear' | 'exponential' = 'linear'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit confused why this needs to be customizable when we always pass down exponential. Should this check be removed?

Copy link

netlify bot commented Sep 9, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 2b66c7c
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/68bff9699eb5c20008b8d0a5
😎 Deploy Preview https://deploy-preview-8548--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

netlify bot commented Sep 9, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit d4cc802
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/68bffa550d544a00083fbe2d
😎 Deploy Preview https://deploy-preview-8548--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

netlify bot commented Sep 9, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 8c98cdb
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/68bffa6e53cc050008637ad6
😎 Deploy Preview https://deploy-preview-8548--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@sheremet-va
Copy link
Member

The tests are failing.

Comment on lines -450 to -456
/**
* Times to retry the test if fails. Useful for making flaky tests more stable.
* When retries is up, the last test error will be thrown.
*
* @default 0
*/
retry?: number
Copy link
Contributor

@flx-sta flx-sta Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the doc-comment be replaced/extended instead of removed completely?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Enhanced retry options
3 participants