Skip to content

Commit

Permalink
fix: support accessing fixture at same index of dependency fixture (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyddall committed Oct 31, 2023
1 parent b8ca387 commit 4cd1d3c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/runner/src/fixture.ts
Expand Up @@ -37,7 +37,7 @@ export function mergeContextFixtures(fixtures: Record<string, any>, context: { f
if (fixture.isFn) {
const usedProps = getUsedProps(fixture.value)
if (usedProps.length)
fixture.deps = context.fixtures!.filter(({ index, prop }) => index !== fixture.index && usedProps.includes(prop))
fixture.deps = context.fixtures!.filter(({ prop }) => prop !== fixture.prop && usedProps.includes(prop))
}
})

Expand Down
29 changes: 8 additions & 21 deletions packages/runner/src/types/tasks.ts
Expand Up @@ -200,28 +200,15 @@ export type TestAPI<ExtraContext = {}> = ChainableTestAPI<ExtraContext> & Extend
K extends keyof ExtraContext ? ExtraContext[K] : never }>
}

type FixtureType<T> = T extends (context: any, use: (fixture: infer F) => any) => any ? F : T
export type Fixture<
T,
K extends keyof T,
OnlyFunction,
ExtraContext = {},
V = FixtureType<T[K]>,
FN = (
context: {
[P in keyof T | keyof ExtraContext as P extends K ?
P extends keyof ExtraContext ? P : never : P
]:
K extends P ? K extends keyof ExtraContext ? ExtraContext[K] : V :
P extends keyof T ? T[P] : never
} & TestContext,
use: (fixture: V) => Promise<void>
) => Promise<void>,
> = OnlyFunction extends true ? FN : (FN | V)
export type Use<T> = (value: T) => Promise<void>
export type FixtureFn<T, K extends keyof T, ExtraContext> =
(context: Omit<T, K> & ExtraContext, use: Use<T[K]>) => Promise<void>
export type Fixture<T, K extends keyof T, ExtraContext = {}> =
((...args: any) => any) extends T[K]
? (T[K] extends any ? FixtureFn<T, K, Omit<ExtraContext, Exclude<keyof T, K>>> : never)
: T[K] | (T[K] extends any ? FixtureFn<T, K, Omit<ExtraContext, Exclude<keyof T, K>>> : never)
export type Fixtures<T extends Record<string, any>, ExtraContext = {}> = {
[K in keyof T]: Fixture<T, K, false, ExtraContext>
} | {
[K in keyof T]: Fixture<T, K, true, ExtraContext>
[K in keyof T]: Fixture<T, K, ExtraContext & ExtendedContext<Test>>
}

export type InferFixturesTypes<T> = T extends TestAPI<infer C> ? C : T
Expand Down
17 changes: 17 additions & 0 deletions test/core/test/fixture-initialization.test.ts
@@ -1,3 +1,4 @@
import type { Use } from '@vitest/runner'
import { describe, expect, expectTypeOf, test, vi } from 'vitest'

interface Fixtures {
Expand Down Expand Up @@ -127,4 +128,20 @@ describe('fixture initialization', () => {
expect(fnD).toBeCalledTimes(1)
})
})

describe('fixture dependency', () => {
const myTest = test
.extend({ a: 1 })
.extend({
b: async ({ a }, use: Use<number>) => {
expectTypeOf(a).toEqualTypeOf<number>()
await use(a * 2)
},
})

myTest('b => a', ({ b }) => {
expectTypeOf(b).toEqualTypeOf<number>()
expect(b).toBe(2)
})
})
})
4 changes: 4 additions & 0 deletions test/core/test/test-extend.test.ts
Expand Up @@ -47,6 +47,7 @@ describe('test.extend()', () => {
string: string
any: any
boolean: boolean
func: (a: number, b: string) => void
}

const typesTest = test.extend<TypesContext>({
Expand All @@ -59,6 +60,9 @@ describe('test.extend()', () => {
await use({})
},
boolean: true,
func: async ({}, use): Promise<void> => {
await use(() => undefined)
},
})

expectTypeOf(typesTest).toEqualTypeOf<TestAPI<InferFixturesTypes<typeof typesTest>>>()
Expand Down

0 comments on commit 4cd1d3c

Please sign in to comment.