Skip to content

Commit

Permalink
Adding test, docs and fixing a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
samkevin1 committed Feb 27, 2023
1 parent d947d9f commit 7991870
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
27 changes: 27 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,33 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t
You cannot use this syntax, when using Vitest as [type checker](/guide/testing-types).
:::

### test.repeats

- **Type:** `(name: string, fn: TestFunction, timeout?: number | TestOptions) => void`
- **Alias:** `it.repeats`

If you want to run a test multiple times to see if it passes on all attempts, you can use `test.repeats` to do so.

By default it will repeat 5 times:

```ts
import { expect, test } from 'vitest'

test.repeats('repeated test', () => {
expect(true).toBe(true)
})
```

To change the default `repeats` value:

```ts
import { expect, test } from 'vitest'

test.repeats('repeated test', () => {
expect(true).toBe(true)
}, { repeats: 3 })
```

## bench

- **Type:** `(name: string, fn: BenchFunction, options?: BenchOptions) => void`
Expand Down
5 changes: 5 additions & 0 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,19 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
}

function initSuite() {
if (typeof suiteOptions === 'number')
suiteOptions = { timeout: suiteOptions }

suite = {
id: '',
type: 'suite',
name,
mode,
shuffle,
tasks: [],
repeats: mode === 'repeats' && !suiteOptions?.repeats ? 5 : suiteOptions?.repeats,
}

setHooks(suite, createSuiteHooks())
}

Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/utils/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function interpretTaskModes(suite: Suite, namePattern?: string | RegExp,

// if all subtasks are skipped, mark as skip
if (suite.mode === 'run') {
if (suite.tasks.length && suite.tasks.every(i => i.mode !== 'run'))
if (suite.tasks.length && suite.tasks.every(i => i.mode !== 'run' && i.mode !== 'repeats'))
suite.mode = 'skip'
}
}
Expand Down
43 changes: 43 additions & 0 deletions test/core/test/repeats.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { afterAll, describe, expect, test } from 'vitest'

const testNumbers: number[] = []

describe('repeat tests', () => {
const result = [1, 1, 1, 1, 1, 2, 2, 2]
// repeats 5 times by default
test.repeats('test 1', () => {
testNumbers.push(1)
})

test.repeats('test 2', () => {
testNumbers.push(2)
}, { repeats: 3 })

test.repeats.fails('test 3', () => {
testNumbers.push(3)
expect(testNumbers).toStrictEqual(result)
})

afterAll(() => {
result.push(3)
expect(testNumbers).toStrictEqual(result)
})
})

const describeNumbers: number[] = []

describe.repeats('repeat tests', () => {
test('test 1', () => {
describeNumbers.push(1)
})
})

describe.repeats('repeat tests', () => {
test('test 2', () => {
describeNumbers.push(2)
})
}, { repeats: 3 })

afterAll(() => {
expect(describeNumbers).toStrictEqual([1, 1, 1, 1, 1, 2, 2, 2])
})

0 comments on commit 7991870

Please sign in to comment.