Skip to content

Commit a92812b

Browse files
authored
fix(runner): preserve handler wrapping on extend (#8153)
1 parent 5600772 commit a92812b

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

packages/runner/src/suite.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,8 @@ export function createTaskCollector(
770770
runner,
771771
)
772772

773-
return createTest(function fn(
773+
const originalWrapper = fn
774+
return createTest(function (
774775
name: string | Function,
775776
optionsOrFn?: TestOptions | TestFunction,
776777
optionsOrTest?: number | TestOptions | TestFunction,
@@ -784,12 +785,9 @@ export function createTaskCollector(
784785
scopedFixtures,
785786
)
786787
}
787-
collector.test.fn.call(
788-
context,
789-
formatName(name),
790-
optionsOrFn as TestOptions,
791-
optionsOrTest as TestFunction,
792-
)
788+
const { handler, options } = parseArguments(optionsOrFn, optionsOrTest)
789+
const timeout = options.timeout ?? runner?.config.testTimeout
790+
originalWrapper.call(context, formatName(name), handler, timeout)
793791
}, _context)
794792
}
795793

test/core/test/task-collector.test.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { expect, test, vi } from 'vitest'
2-
import { createTaskCollector } from 'vitest/suite'
1+
import { assert, describe, expect, test, vi } from 'vitest'
2+
import { createTaskCollector, getCurrentSuite } from 'vitest/suite'
33

44
test('collector keeps the order of arguments', () => {
55
const fn = vi.fn()
@@ -23,3 +23,29 @@ test('collector keeps the order of arguments', () => {
2323

2424
expect(fn).toHaveBeenNthCalledWith(4, 'a', options, expect.any(Function))
2525
})
26+
27+
describe('collector.extend should preserve handler wrapping', () => {
28+
let flag = false
29+
30+
const flagTest = createTaskCollector(function (
31+
this: object,
32+
name: string,
33+
fn: () => void,
34+
) {
35+
const handler = async () => {
36+
flag = false
37+
await fn()
38+
assert(flag)
39+
}
40+
getCurrentSuite().task(name, { ...this, handler })
41+
})
42+
43+
const extendedTest = flagTest.extend({})
44+
45+
extendedTest.fails('should fail when flag is never set', {}, () => {})
46+
47+
flagTest('should pass when flag is set', () => {
48+
flag = true
49+
expect(flag).toBe(true)
50+
})
51+
})

0 commit comments

Comments
 (0)