Skip to content

Commit

Permalink
feat: support readonly tuples as input to .each
Browse files Browse the repository at this point in the history
Using readonly tuples makes it easy to type check their individual parts inside the test.
  • Loading branch information
IgnusG committed Feb 11, 2022
1 parent 597db8c commit ac7e256
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
10 changes: 5 additions & 5 deletions packages/vitest/src/runtime/suite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { format } from 'util'
import type { File, RunMode, Suite, SuiteAPI, SuiteCollector, SuiteFactory, SuiteHooks, Test, TestAPI, TestFunction } from '../types'
import type { File, MutableArray, RunMode, Suite, SuiteAPI, SuiteCollector, SuiteFactory, SuiteHooks, Test, TestAPI, TestFunction } from '../types'
import { isObject, noop, toArray } from '../utils'
import { createChainable } from './chain'
import { collectTask, context, normalizeTest, runWithSuite } from './context'
Expand Down Expand Up @@ -145,8 +145,8 @@ function createSuite() {
},
) as SuiteAPI

suite.each = <T>(cases: T[]) => {
return (name: string, fn: (...args: T extends any[] ? T : [T]) => void) => {
suite.each = <T>(cases: T[] | readonly T[]) => {
return (name: string, fn: (...args: T extends any[] | readonly any[] ? MutableArray<T> : [T]) => void) => {
cases.forEach((i) => {
const items = toArray(i) as any
suite(formatTitle(name, items), () => fn(...items))
Expand All @@ -163,8 +163,8 @@ function createTest(fn: ((this: Record<'concurrent'| 'skip'| 'only'| 'todo'| 'fa
fn,
) as TestAPI

test.each = <T>(cases: T[]) => {
return (name: string, fn: (...args: T extends any[] ? T : [T]) => void) => {
test.each = <T>(cases: T[] | readonly T[]) => {
return (name: string, fn: (...args: T extends any[] | readonly any[] ? MutableArray<T> : [T]) => void) => {
cases.forEach((i) => {
const items = toArray(i) as any
test(formatTitle(name, items), () => fn(...items))
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/types/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export type DeepMerge<F, S> = MergeInsertions<{
: never;
}>

export type MutableArray<T extends readonly any[]> = { -readonly [k in keyof T]: T[k] }

export interface Constructable {
new (...args: any[]): any
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/types/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ChainableFunction } from '../runtime/chain'
import type { Awaitable, ErrorWithDiff } from './general'
import type { Awaitable, ErrorWithDiff, MutableArray } from './general'
import type { UserConsoleLog } from '.'

export type RunMode = 'run' | 'skip' | 'only' | 'todo'
Expand Down Expand Up @@ -44,7 +44,7 @@ export type Task = Test | Suite | File

export type DoneCallback = (error?: any) => void
export type TestFunction = (done: DoneCallback) => Awaitable<void>
export type EachFunction = <T>(cases: T[]) => (name: string, fn: (...args: T extends any[] ? T : [T]) => void) => void
export type EachFunction = <T>(cases: T[] | readonly T[]) => (name: string, fn: (...args: T extends any[] | readonly any[] ? MutableArray<T> : [T]) => void) => void

export type TestAPI = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'fails',
Expand Down

0 comments on commit ac7e256

Please sign in to comment.