Skip to content

Commit

Permalink
fix: keep order of arguments for .each in custom task collectors (#5640)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Apr 30, 2024
1 parent 959247e commit 7d57c11
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
30 changes: 24 additions & 6 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,20 @@ function createSuite() {
fnOrOptions,
)

const fnFirst = typeof optionsOrFn === 'function'

cases.forEach((i, idx) => {
const items = Array.isArray(i) ? i : [i]
arrayOnlyCases
? suite(formatTitle(_name, items, idx), options, () => handler(...items))
: suite(formatTitle(_name, items, idx), options, () => handler(i))
if (fnFirst) {
arrayOnlyCases
? suite(formatTitle(_name, items, idx), () => handler(...items), options)
: suite(formatTitle(_name, items, idx), () => handler(i), options)
}
else {
arrayOnlyCases
? suite(formatTitle(_name, items, idx), options, () => handler(...items))
: suite(formatTitle(_name, items, idx), options, () => handler(i))
}
})

this.setContext('each', undefined)
Expand Down Expand Up @@ -341,12 +350,21 @@ export function createTaskCollector(
fnOrOptions,
)

const fnFirst = typeof optionsOrFn === 'function'

cases.forEach((i, idx) => {
const items = Array.isArray(i) ? i : [i]

arrayOnlyCases
? test(formatTitle(_name, items, idx), options, () => handler(...items))
: test(formatTitle(_name, items, idx), options, () => handler(i))
if (fnFirst) {
arrayOnlyCases
? test(formatTitle(_name, items, idx), () => handler(...items), options)
: test(formatTitle(_name, items, idx), () => handler(i), options)
}
else {
arrayOnlyCases
? test(formatTitle(_name, items, idx), options, () => handler(...items))
: test(formatTitle(_name, items, idx), options, () => handler(i))
}
})

this.setContext('each', undefined)
Expand Down
25 changes: 25 additions & 0 deletions test/core/test/task-collector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect, test, vi } from 'vitest'
import { createTaskCollector } from 'vitest/suite'

test('collector keeps the order of arguments', () => {
const fn = vi.fn()
const collector = createTaskCollector(fn)
const cb = vi.fn()
const options = {}

collector('a', cb, options)

expect(fn).toHaveBeenNthCalledWith(1, 'a', cb, options)

collector('a', options, cb)

expect(fn).toHaveBeenNthCalledWith(2, 'a', options, cb)

collector.each([1])('a', cb, options)

expect(fn).toHaveBeenNthCalledWith(3, 'a', expect.any(Function), options)

collector.each([1])('a', options, cb)

expect(fn).toHaveBeenNthCalledWith(4, 'a', options, expect.any(Function))
})

0 comments on commit 7d57c11

Please sign in to comment.