Skip to content

Commit

Permalink
feat: allow timeout in test.each (#1787)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Aug 5, 2022
1 parent 4a6aeb6 commit 1a0d34b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
6 changes: 3 additions & 3 deletions packages/vitest/src/runtime/suite.ts
Expand Up @@ -166,7 +166,7 @@ function createSuite() {
const suite = createChainable(
['concurrent', 'shuffle', 'skip', 'only', 'todo'],
function (name: string, factory?: SuiteFactory) {
const mode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'
const mode: RunMode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'
return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle)
},
) as SuiteAPI
Expand Down Expand Up @@ -200,10 +200,10 @@ function createTest(fn: (
) as TestAPI

test.each = <T>(cases: ReadonlyArray<T>) => {
return (name: string, fn: (...args: T[]) => void) => {
return (name: string, fn: (...args: T[]) => void, timeout?: number) => {
cases.forEach((i, idx) => {
const items = Array.isArray(i) ? i : [i]
test(formatTitle(name, items, idx), () => fn(...items))
test(formatTitle(name, items, idx), () => fn(...items), timeout)
})
}
}
Expand Down
42 changes: 30 additions & 12 deletions packages/vitest/src/types/tasks.ts
Expand Up @@ -89,41 +89,59 @@ type ExtractEachCallbackArgs<T extends ReadonlyArray<any>> = {
? 10
: 'fallback']

interface EachFunction {
interface SuiteEachFunction {
<T extends any[] | [any]>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: T) => Awaitable<void>
fn: (...args: T) => Awaitable<void>,
) => void
<T extends ReadonlyArray<any>>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: ExtractEachCallbackArgs<T>) => Awaitable<void>
fn: (...args: ExtractEachCallbackArgs<T>) => Awaitable<void>,
) => void
<T>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: T[]) => Awaitable<void>
fn: (...args: T[]) => Awaitable<void>,
) => void
}

interface TestEachFunction {
<T extends any[] | [any]>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: T) => Awaitable<void>,
timeout?: number,
) => void
<T extends ReadonlyArray<any>>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: ExtractEachCallbackArgs<T>) => Awaitable<void>,
timeout?: number,
) => void
<T>(cases: ReadonlyArray<T>): (
name: string,
fn: (...args: T[]) => Awaitable<void>,
timeout?: number,
) => void
}

type ChainableTestAPI<ExtraContext = {}> = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'fails',
[name: string, fn?: TestFunction<ExtraContext>, timeout?: number],
void
'concurrent' | 'only' | 'skip' | 'todo' | 'fails',
[name: string, fn?: TestFunction<ExtraContext>, timeout?: number],
void
>

export type TestAPI<ExtraContext = {}> = ChainableTestAPI<ExtraContext> & {
each: EachFunction
each: TestEachFunction
skipIf(condition: any): ChainableTestAPI<ExtraContext>
runIf(condition: any): ChainableTestAPI<ExtraContext>
}

type ChainableSuiteAPI<ExtraContext = {}> = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'shuffle',
[name: string, factory?: SuiteFactory],
SuiteCollector<ExtraContext>
'concurrent' | 'only' | 'skip' | 'todo' | 'shuffle',
[name: string, factory?: SuiteFactory],
SuiteCollector<ExtraContext>
>

export type SuiteAPI<ExtraContext = {}> = ChainableSuiteAPI & {
each: EachFunction
each: SuiteEachFunction
skipIf(condition: any): ChainableSuiteAPI<ExtraContext>
runIf(condition: any): ChainableSuiteAPI<ExtraContext>
}
Expand Down
5 changes: 5 additions & 0 deletions test/fails/fixtures/each-timeout.test.ts
@@ -0,0 +1,5 @@
import { test } from 'vitest'

test.each([1])('test each timeout', async () => {
await new Promise(resolve => setTimeout(resolve, 20))
}, 10)
2 changes: 2 additions & 0 deletions test/fails/test/__snapshots__/runner.test.ts.snap
@@ -1,5 +1,7 @@
// Vitest Snapshot v1

exports[`should fails > each-timeout.test.ts > each-timeout.test.ts 1`] = `"Error: Test timed out in 10ms."`;

exports[`should fails > empty.test.ts > empty.test.ts 1`] = `"Error: No test suite found in file <rootDir>/empty.test.ts"`;
exports[`should fails > expect.test.ts > expect.test.ts 1`] = `"AssertionError: expected 2 to deeply equal 3"`;
Expand Down

0 comments on commit 1a0d34b

Please sign in to comment.