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

feat: allow timeout in test.each #1787

Merged
merged 1 commit into from Aug 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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