diff --git a/packages/runner/src/suite.ts b/packages/runner/src/suite.ts index e946384cad4f..80d5ee283628 100644 --- a/packages/runner/src/suite.ts +++ b/packages/runner/src/suite.ts @@ -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) @@ -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) diff --git a/test/core/test/task-collector.test.ts b/test/core/test/task-collector.test.ts new file mode 100644 index 000000000000..f37009f40c07 --- /dev/null +++ b/test/core/test/task-collector.test.ts @@ -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)) +})