MCPcopy
hub / github.com/vitest-dev/vitest / createTaskCollector

Function createTaskCollector

packages/runner/src/suite.ts:744–976  ·  view source on GitHub ↗
(
  fn: (...args: any[]) => any,
)

Source from the content-addressed store, hash-verified

742}
743
744export function createTaskCollector(
745 fn: (...args: any[]) => any,
746): TestAPI {
747 const taskFn = fn as any
748
749 taskFn.each = function <T>(
750 this: TestAPI,
751 cases: ReadonlyArray<T>,
752 ...args: any[]
753 ) {
754 const context = getChainableContext(this)
755 const test = context.withContext()
756 context.setContext('each', true)
757
758 if (Array.isArray(cases) && args.length) {
759 cases = formatTemplateString(cases, args)
760 }
761
762 return (
763 name: string | Function,
764 optionsOrFn: ((...args: T[]) => void) | TestOptions,
765 fnOrOptions?: ((...args: T[]) => void) | number,
766 ) => {
767 const _name = formatName(name)
768 const arrayOnlyCases = cases.every(Array.isArray)
769
770 const { options, handler } = parseArguments(optionsOrFn, fnOrOptions)
771
772 const fnFirst = typeof optionsOrFn === 'function'
773
774 cases.forEach((i, idx) => {
775 const items = Array.isArray(i) ? i : [i]
776
777 if (fnFirst) {
778 if (arrayOnlyCases) {
779 test(
780 formatTitle(_name, items, idx),
781 handler ? () => handler(...items) : undefined,
782 options.timeout,
783 )
784 }
785 else {
786 test(formatTitle(_name, items, idx), handler ? () => handler(i) : undefined, options.timeout)
787 }
788 }
789 else {
790 if (arrayOnlyCases) {
791 test(formatTitle(_name, items, idx), options, handler ? () => handler(...items) : undefined)
792 }
793 else {
794 test(formatTitle(_name, items, idx), options, handler ? () => handler(i) : undefined)
795 }
796 }
797 })
798
799 context.setContext('each', undefined)
800 }
801 }

Callers 1

createTestFunction · 0.85

Calls 15

getChainableContextFunction · 0.90
configurePropsFunction · 0.90
toArrayFunction · 0.90
createChainableFunction · 0.90
formatTemplateStringFunction · 0.85
formatTitleFunction · 0.85
parseBuilderFixturesFunction · 0.85
overrideMethod · 0.80
warnMethod · 0.80
formatNameFunction · 0.70
parseArgumentsFunction · 0.70
createTestFunction · 0.70

Tested by

no test coverage detected