Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: propagate describe options #2025

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ When you use `test` in the top level of file, they are collected as part of the

### describe.skip

- **Type:** `(name: string, fn: TestFunction) => void`
- **Type:** `(name: string, fn: TestFunction, options?: number | TestOptions) => void`

Use `describe.skip` in a suite to avoid running a particular describe block.

Expand All @@ -285,7 +285,7 @@ When you use `test` in the top level of file, they are collected as part of the

### describe.only

- **Type:** `(name: string, fn: TestFunction) => void`
- **Type:** `(name: string, fn: TestFunction, options?: number | TestOptions) => void`

Use `describe.only` to only run certain suites

Expand All @@ -311,7 +311,7 @@ When you use `test` in the top level of file, they are collected as part of the

### describe.concurrent

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

`describe.concurrent` in a suite marks every tests as concurrent

Expand All @@ -335,7 +335,7 @@ When you use `test` in the top level of file, they are collected as part of the

### describe.shuffle

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

Vitest provides a way to run all tests in random order via CLI flag [`--sequence.shuffle`](/guide/cli) or config option [`sequence.shuffle`](/config/#sequence-shuffle), but if you want to have only part of your test suite to run tests in random order, you can mark it with this flag.

Expand All @@ -362,7 +362,7 @@ When you use `test` in the top level of file, they are collected as part of the
```
### describe.each

- **Type:** `(cases: ReadonlyArray<T>): (name: string, fn: (...args: T[]) => void) => void`
- **Type:** `(cases: ReadonlyArray<T>): (name: string, fn: (...args: T[]) => void, options?: number | TestOptions) => void`

Use `describe.each` if you have more than one test that depends on the same data.

Expand Down
13 changes: 6 additions & 7 deletions packages/vitest/src/runtime/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ export function createSuiteHooks() {
}
}

function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, mode: RunMode, concurrent?: boolean, shuffle?: boolean) {
function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, mode: RunMode, concurrent?: boolean, shuffle?: boolean, suiteOptions?: number | TestOptions) {
const tasks: (Benchmark | Test | Suite | SuiteCollector)[] = []
const factoryQueue: (Test | Suite | SuiteCollector)[] = []

let suite: Suite

initSuite()

const test = createTest(function (name: string, fn = noop, options?: number | TestOptions) {
const test = createTest(function (name: string, fn = noop, options = suiteOptions) {
if (!isRunningInTest())
throw new Error('`test()` and `it()` is only available in test mode.')

Expand Down Expand Up @@ -191,22 +191,21 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
}

collectTask(collector)

return collector
}

function createSuite() {
function suiteFn(this: Record<string, boolean | undefined>, name: string, factory?: SuiteFactory) {
function suiteFn(this: Record<string, boolean | undefined>, name: string, factory?: SuiteFactory, options?: number | TestOptions) {
const mode: RunMode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'
return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle)
return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle, options)
}

suiteFn.each = function<T>(this: { withContext: () => SuiteAPI }, cases: ReadonlyArray<T>) {
const suite = this.withContext()
return (name: string, fn: (...args: T[]) => void) => {
return (name: string, fn: (...args: T[]) => void, options?: number | TestOptions) => {
cases.forEach((i, idx) => {
const items = Array.isArray(i) ? i : [i]
suite(formatTitle(name, items, idx), () => fn(...items))
suite(formatTitle(name, items, idx), () => fn(...items), options)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export type TestAPI<ExtraContext = {}> = ChainableTestAPI<ExtraContext> & {

type ChainableSuiteAPI<ExtraContext = {}> = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'shuffle',
[name: string, factory?: SuiteFactory<ExtraContext>],
[name: string, factory?: SuiteFactory<ExtraContext>, options?: number | TestOptions],
SuiteCollector<ExtraContext>,
{
each: TestEachFunction
Expand Down
15 changes: 15 additions & 0 deletions test/core/test/retry-only.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { describe, expect, it } from 'vitest'

describe.only('description.only retry', () => {
let count4 = 0
let count5 = 0
it('test should inherit options from the description block if missing', () => {
count4 += 1
expect(count4).toBe(2)
})

it('test should not inherit options from the description block if exists', () => {
count5 += 1
expect(count5).toBe(5)
}, { retry: 5 })
}, { retry: 2 })
43 changes: 42 additions & 1 deletion test/core/test/retry.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, it } from 'vitest'
import { describe, expect, it } from 'vitest'

let count1 = 0
it('retry test', () => {
Expand All @@ -23,3 +23,44 @@ it('result', () => {
expect(count2).toEqual(2)
expect(count3).toEqual(3)
})

describe('description retry', () => {
let count4 = 0
let count5 = 0
it('test should inherit options from the description block if missing', () => {
count4 += 1
expect(count4).toBe(2)
})

it('test should not inherit options from the description block if exists', () => {
count5 += 1
expect(count5).toBe(5)
}, { retry: 5 })
}, { retry: 2 })

describe.each([
{ a: 1, b: 1, expected: 2 },
{ a: 1, b: 2, expected: 3 },
{ a: 2, b: 1, expected: 3 },
])('describe object add($a, $b)', ({ a, b, expected }) => {
let flag1 = false
let flag2 = false
let flag3 = false
it(`returns ${expected}`, () => {
flag1 = !flag1
expect(a + b).toBe(expected)
expect(flag1).toBe(false)
})

it(`returned value not be greater than ${expected}`, () => {
flag2 = !flag2
expect(a + b).not.toBeGreaterThan(expected)
expect(flag2).toBe(false)
})

it(`returned value not be less than ${expected}`, () => {
flag3 = !flag3
expect(a + b).not.toBeLessThan(expected)
expect(flag3).toBe(false)
})
}, { retry: 2 })