[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

fix: keep order of arguments for .each in custom task collectors (#5640)

authored by

Vladimir and committed by
GitHub
(Apr 30, 2024, 12:28 PM +0200) 7d57c116 959247e9

+49 -6
+24 -6
packages/runner/src/suite.ts
··· 299 299 fnOrOptions, 300 300 ) 301 301 302 + const fnFirst = typeof optionsOrFn === 'function' 303 + 302 304 cases.forEach((i, idx) => { 303 305 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 + } 307 316 }) 308 317 309 318 this.setContext('each', undefined) ··· 341 350 fnOrOptions, 342 351 ) 343 352 353 + const fnFirst = typeof optionsOrFn === 'function' 354 + 344 355 cases.forEach((i, idx) => { 345 356 const items = Array.isArray(i) ? i : [i] 346 357 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 + } 350 368 }) 351 369 352 370 this.setContext('each', undefined)
+25
test/core/test/task-collector.test.ts
··· 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 + })