Skip to content

Commit 7d57c11

Browse files
authoredApr 30, 2024··
fix: keep order of arguments for .each in custom task collectors (#5640)
1 parent 959247e commit 7d57c11

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed
 

‎packages/runner/src/suite.ts

+24-6
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,20 @@ function createSuite() {
299299
fnOrOptions,
300300
)
301301

302+
const fnFirst = typeof optionsOrFn === 'function'
303+
302304
cases.forEach((i, idx) => {
303305
const items = Array.isArray(i) ? i : [i]
304-
arrayOnlyCases
305-
? suite(formatTitle(_name, items, idx), options, () => handler(...items))
306-
: suite(formatTitle(_name, items, idx), options, () => handler(i))
306+
if (fnFirst) {
307+
arrayOnlyCases
308+
? suite(formatTitle(_name, items, idx), () => handler(...items), options)
309+
: suite(formatTitle(_name, items, idx), () => handler(i), options)
310+
}
311+
else {
312+
arrayOnlyCases
313+
? suite(formatTitle(_name, items, idx), options, () => handler(...items))
314+
: suite(formatTitle(_name, items, idx), options, () => handler(i))
315+
}
307316
})
308317

309318
this.setContext('each', undefined)
@@ -341,12 +350,21 @@ export function createTaskCollector(
341350
fnOrOptions,
342351
)
343352

353+
const fnFirst = typeof optionsOrFn === 'function'
354+
344355
cases.forEach((i, idx) => {
345356
const items = Array.isArray(i) ? i : [i]
346357

347-
arrayOnlyCases
348-
? test(formatTitle(_name, items, idx), options, () => handler(...items))
349-
: test(formatTitle(_name, items, idx), options, () => handler(i))
358+
if (fnFirst) {
359+
arrayOnlyCases
360+
? test(formatTitle(_name, items, idx), () => handler(...items), options)
361+
: test(formatTitle(_name, items, idx), () => handler(i), options)
362+
}
363+
else {
364+
arrayOnlyCases
365+
? test(formatTitle(_name, items, idx), options, () => handler(...items))
366+
: test(formatTitle(_name, items, idx), options, () => handler(i))
367+
}
350368
})
351369

352370
this.setContext('each', undefined)

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

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { expect, test, vi } from 'vitest'
2+
import { createTaskCollector } from 'vitest/suite'
3+
4+
test('collector keeps the order of arguments', () => {
5+
const fn = vi.fn()
6+
const collector = createTaskCollector(fn)
7+
const cb = vi.fn()
8+
const options = {}
9+
10+
collector('a', cb, options)
11+
12+
expect(fn).toHaveBeenNthCalledWith(1, 'a', cb, options)
13+
14+
collector('a', options, cb)
15+
16+
expect(fn).toHaveBeenNthCalledWith(2, 'a', options, cb)
17+
18+
collector.each([1])('a', cb, options)
19+
20+
expect(fn).toHaveBeenNthCalledWith(3, 'a', expect.any(Function), options)
21+
22+
collector.each([1])('a', options, cb)
23+
24+
expect(fn).toHaveBeenNthCalledWith(4, 'a', options, expect.any(Function))
25+
})

0 commit comments

Comments
 (0)
Please sign in to comment.