docs/config/retry.md
Retry the test specific number of times if it fails.
number | { count?: number, delay?: number, condition?: RegExp }0--retry <times>, --retry.count <times>, --retry.delay <ms>, --retry.condition <pattern>Specify a number to retry failed tests:
export default defineConfig({
test: {
retry: 3,
},
})
You can also configure retry options from the command line:
# Simple retry count
vitest --retry 3
# Advanced options using dot notation
vitest --retry.count 3 --retry.delay 500 --retry.condition 'ECONNREFUSED|timeout'
Use an object to configure retry behavior:
export default defineConfig({
test: {
retry: {
count: 3, // Number of times to retry
delay: 1000, // Delay in milliseconds between retries
condition: /ECONNREFUSED|timeout/i, // RegExp to match errors that should trigger retry
},
},
})
Number of times to retry a test if it fails. Default is 0.
export default defineConfig({
test: {
retry: {
count: 2,
},
},
})
Delay in milliseconds between retry attempts. Useful for tests that interact with rate-limited APIs or need time to recover. Default is 0.
export default defineConfig({
test: {
retry: {
count: 3,
delay: 500, // Wait 500ms between retries
},
},
})
A RegExp pattern or a function to determine if a test should be retried based on the error.
::: warning
When defining condition as a function, it must be done in a test file directly, not in a configuration file (configurations are serialized for worker threads).
:::
export default defineConfig({
test: {
retry: {
count: 2,
condition: /ECONNREFUSED|ETIMEDOUT/i, // Retry on connection/timeout errors
},
},
})
import { describe, test } from 'vitest'
describe('tests with advanced retry condition', () => {
test('with function condition', { retry: { count: 2, condition: error => error.message.includes('Network') } }, () => {
// test code
})
})
You can also define retry options per test or suite in test files:
import { describe, test } from 'vitest'
describe('flaky tests', {
retry: {
count: 2,
delay: 100,
},
}, () => {
test('network request', () => {
// test code
})
})
test('another test', {
retry: {
count: 3,
condition: error => error.message.includes('timeout'),
},
}, () => {
// test code
})