|
| 1 | +import { getFixture } from './map' |
1 | 2 | import type { TestContext } from './types' |
2 | 3 |
|
3 | 4 | export interface FixtureItem { |
@@ -43,32 +44,41 @@ export function mergeContextFixtures(fixtures: Record<string, any>, context: { f |
43 | 44 | return context |
44 | 45 | } |
45 | 46 |
|
46 | | -export function withFixtures(fn: Function, fixtures: FixtureItem[], context: TestContext & Record<string, any>) { |
47 | | - if (!fixtures.length) |
48 | | - return () => fn(context) |
| 47 | +export function withFixtures(fn: Function, testContext?: TestContext) { |
| 48 | + return (hookContext?: TestContext) => { |
| 49 | + const context: TestContext & { [key: string]: any } | undefined = hookContext || testContext |
49 | 50 |
|
50 | | - const usedProps = getUsedProps(fn) |
51 | | - if (!usedProps.length) |
52 | | - return () => fn(context) |
| 51 | + if (!context) |
| 52 | + return fn({}) |
53 | 53 |
|
54 | | - const usedFixtures = fixtures.filter(({ prop }) => usedProps.includes(prop)) |
55 | | - const pendingFixtures = resolveDeps(usedFixtures) |
56 | | - let cursor = 0 |
| 54 | + const fixtures = getFixture(context) |
| 55 | + if (!fixtures?.length) |
| 56 | + return fn(context) |
57 | 57 |
|
58 | | - async function use(fixtureValue: any) { |
59 | | - const { prop } = pendingFixtures[cursor++] |
60 | | - context[prop] = fixtureValue |
61 | | - if (cursor < pendingFixtures.length) |
62 | | - await next() |
63 | | - else await fn(context) |
64 | | - } |
| 58 | + const usedProps = getUsedProps(fn) |
| 59 | + if (!usedProps.length) |
| 60 | + return fn(context) |
65 | 61 |
|
66 | | - async function next() { |
67 | | - const { value } = pendingFixtures[cursor] |
68 | | - typeof value === 'function' ? await value(context, use) : await use(value) |
69 | | - } |
| 62 | + const usedFixtures = fixtures.filter(({ prop }) => usedProps.includes(prop)) |
| 63 | + const pendingFixtures = resolveDeps(usedFixtures) |
| 64 | + let cursor = 0 |
| 65 | + |
| 66 | + async function use(fixtureValue: any) { |
| 67 | + const { prop } = pendingFixtures[cursor++] |
| 68 | + context![prop] = fixtureValue |
| 69 | + |
| 70 | + if (cursor < pendingFixtures.length) |
| 71 | + await next() |
| 72 | + else await fn(context) |
| 73 | + } |
| 74 | + |
| 75 | + async function next() { |
| 76 | + const { value } = pendingFixtures[cursor] |
| 77 | + typeof value === 'function' ? await value(context, use) : await use(value) |
| 78 | + } |
70 | 79 |
|
71 | | - return () => next() |
| 80 | + return next() |
| 81 | + } |
72 | 82 | } |
73 | 83 |
|
74 | 84 | function resolveDeps(fixtures: FixtureItem[], depSet = new Set<FixtureItem>(), pendingFixtures: FixtureItem[] = []) { |
|
0 commit comments