[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

feat(runner): improve text.extend types (#4176)

authored by

Dunqing and committed by
GitHub
(Sep 27, 2023, 2:30 PM +0200) bbb43fb1 f8dacb2c

+50 -7
+27
test/core/test/test-extend.test.ts
··· 1 1 /* eslint-disable prefer-rest-params */ 2 2 /* eslint-disable no-empty-pattern */ 3 + import type { InferFixturesTypes } from '@vitest/runner' 4 + import type { TestAPI } from 'vitest' 3 5 import { afterAll, afterEach, beforeEach, describe, expect, expectTypeOf, test, vi } from 'vitest' 4 6 5 7 interface Fixtures { ··· 38 40 }) 39 41 40 42 describe('test.extend()', () => { 43 + test('types', () => { 44 + interface TypesContext { 45 + number: number 46 + array: number[] 47 + string: string 48 + any: any 49 + boolean: boolean 50 + } 51 + 52 + const typesTest = test.extend<TypesContext>({ 53 + number: 1, 54 + array: [1, 2, 3], 55 + async string({ }, use) { 56 + await use('string') 57 + }, 58 + async any({}, use) { 59 + await use({}) 60 + }, 61 + boolean: true, 62 + }) 63 + 64 + expectTypeOf(typesTest).toEqualTypeOf<TestAPI<InferFixturesTypes<typeof typesTest>>>() 65 + }) 66 + 41 67 describe('basic', () => { 42 68 myTest('todoList and doneList', ({ todoList, doneList, archiveList }) => { 43 69 expect(todoFn).toBeCalledTimes(1) ··· 66 92 archiveList.pop() 67 93 }) 68 94 }) 95 + 69 96 describe('smartly init fixtures', () => { 70 97 myTest('should not init any fixtures', function () { 71 98 expect(todoFn).not.toBeCalled()
+23 -7
packages/runner/src/types/tasks.ts
··· 189 189 K extends keyof ExtraContext ? ExtraContext[K] : never }> 190 190 } 191 191 192 + type FixtureType<T> = T extends (context: any, use: (fixture: infer F) => any) => any ? F : T 193 + export type Fixture< 194 + T, 195 + K extends keyof T, 196 + OnlyFunction, 197 + ExtraContext = {}, 198 + V = FixtureType<T[K]>, 199 + FN = ( 200 + context: { 201 + [P in keyof T | keyof ExtraContext as P extends K ? 202 + P extends keyof ExtraContext ? P : never : P 203 + ]: 204 + K extends P ? K extends keyof ExtraContext ? ExtraContext[K] : V : 205 + P extends keyof T ? T[P] : never 206 + } & TestContext, 207 + use: (fixture: V) => Promise<void> 208 + ) => Promise<void>, 209 + > = OnlyFunction extends true ? FN : (FN | V) 192 210 export type Fixtures<T extends Record<string, any>, ExtraContext = {}> = { 193 - [K in keyof T]: T[K] | ((context: { 194 - [P in keyof T | keyof ExtraContext as P extends K ? 195 - P extends keyof ExtraContext ? P : never : P 196 - ]: 197 - K extends P ? K extends keyof ExtraContext ? ExtraContext[K] : never : 198 - P extends keyof T ? T[P] : never 199 - } & TestContext, use: (fixture: T[K]) => Promise<void>) => Promise<void>) 211 + [K in keyof T]: Fixture<T, K, false, ExtraContext> 212 + } | { 213 + [K in keyof T]: Fixture<T, K, true, ExtraContext> 200 214 } 215 + 216 + export type InferFixturesTypes<T> = T extends TestAPI<infer C> ? C : T 201 217 202 218 type ChainableSuiteAPI<ExtraContext = {}> = ChainableFunction< 203 219 'concurrent' | 'sequential' | 'only' | 'skip' | 'todo' | 'shuffle',