Skip to content

Commit 9a11762

Browse files
HenryZhang-ZHYsheremet-va
andauthoredJun 27, 2023
feat: introduce global configuration for retry setting (fix #3598) (#3603)
Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
1 parent 6ad8ff6 commit 9a11762

File tree

9 files changed

+64
-1
lines changed

9 files changed

+64
-1
lines changed
 

‎docs/config/index.md

+9
Original file line numberDiff line numberDiff line change
@@ -1452,3 +1452,12 @@ This config option affects truncating values in `test.each` titles and inside th
14521452
Stop test execution when given number of tests have failed.
14531453

14541454
By default Vitest will run all of your test cases even if some of them fail. This may not be desired for CI builds where you are only interested in 100% successful builds and would like to stop test execution as early as possible when test failures occur. The `bail` option can be used to speed up CI runs by preventing it from running more tests when failures have occured.
1455+
1456+
### retry
1457+
1458+
- **Type:** `number`
1459+
- **Default:** `0`
1460+
- **CLI:** `--retry=<value>`
1461+
- **Version:** Since Vitest 0.32.3
1462+
1463+
Retry the test specific number of times if it fails.

‎docs/guide/cli.md

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Run only [benchmark](https://vitest.dev/guide/features.html#benchmarking-experim
9393
| `--inspect` | Enables Node.js inspector |
9494
| `--inspect-brk` | Enables Node.js inspector with break |
9595
| `--bail <number>` | Stop test execution when given number of tests have failed |
96+
| `--retry <times>` | Retry the test specific number of times if it fails |
9697
| `-h, --help` | Display available CLI options |
9798

9899
::: tip

‎packages/runner/src/suite.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
7878
mode,
7979
suite: undefined!,
8080
fails: this.fails,
81-
retry: options?.retry,
81+
retry: options?.retry ?? runner.config.retry,
8282
repeats: options?.repeats,
8383
meta: Object.create(null),
8484
} as Omit<Test, 'context'> as Test

‎packages/runner/src/types/runner.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface VitestRunnerConfig {
2020
maxConcurrency: number
2121
testTimeout: number
2222
hookTimeout: number
23+
retry: number
2324
}
2425

2526
export type VitestRunnerImportSource = 'collect' | 'setup'

‎packages/vitest/src/node/cli.ts

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ cli
4848
.option('--inspect-brk', 'Enable Node.js inspector with break')
4949
.option('--test-timeout <time>', 'Default timeout of a test in milliseconds (default: 5000)')
5050
.option('--bail <number>', 'Stop test execution when given number of tests have failed', { default: 0 })
51+
.option('--retry <times>', 'Retry the test specific number of times if it fails', { default: 0 })
5152
.help()
5253

5354
cli

‎packages/vitest/src/types/config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,13 @@ export interface InlineConfig {
597597
* Stop test execution when given number of tests have failed.
598598
*/
599599
bail?: number
600+
601+
/**
602+
* Retry the test specific number of times if it fails.
603+
*
604+
* @default 0
605+
*/
606+
retry?: number
600607
}
601608

602609
export interface TypecheckConfig {
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect, test } from 'vitest'
2+
3+
let number = 0
4+
test('should passed', () => {
5+
expect(number++).toBe(3)
6+
})
7+
8+
test('retry but still failed', () => {
9+
expect(number++).toBe(4)
10+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
include: ['*.test.ts'],
6+
retry: 3,
7+
},
8+
})

‎test/config/test/retry.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import path from 'node:path'
2+
import { describe, expect, test } from 'vitest'
3+
import { runVitest } from '../../test-utils'
4+
5+
const root = path.resolve('./fixtures/retry')
6+
function run(testNamePattern?: string) {
7+
return runVitest({
8+
root,
9+
testNamePattern,
10+
})
11+
}
12+
13+
describe('retry', () => {
14+
test('should passed', async () => {
15+
const { stdout } = await run('should passed')
16+
expect(stdout).toContain('1 passed')
17+
})
18+
19+
test('retry but still failed', async () => {
20+
const { stdout } = await run('retry but still failed')
21+
expect(stdout).toContain('expected 1 to be 4')
22+
expect(stdout).toContain('expected 2 to be 4')
23+
expect(stdout).toContain('expected 3 to be 4')
24+
expect(stdout).toContain('1 failed')
25+
})
26+
})

0 commit comments

Comments
 (0)
Please sign in to comment.