import { invariant, isFunction } from 'vest-utils'; import { IsolateEach, TIsolateEach, } from '../core/isolate/IsolateEach/IsolateEach'; import { ErrorStrings } from '../errors/ErrorStrings'; /** * Iterates over an array of items, allowing to run tests individually per item. * * Requires setting a "key" property on each item tested. * * @example * * each(itemsArray, (item) => { * test(item.name, 'Item value must not be empty', () => { * enforce(item.value).isNotEmpty(); * }, item.id) * }) */ export function each( list: T[], callback: (arg: T, index: number) => void, ): TIsolateEach { invariant( isFunction(callback), ErrorStrings.EACH_CALLBACK_MUST_BE_A_FUNCTION, ); return IsolateEach(() => { list.forEach((arg, index) => { callback(arg, index); }); }); }