Vitest Utils
Utilities for working with vitest.
Example Usage
import { it, expect } from 'vitest';
import { repeatSyncFn, repeatAsyncFn } from './vitest.utils';
// repeatSyncFn
it('use repeatSyncFn', () => {
repeatSyncFn(10_000, () => {
expect(true).toBe(true);
});
}))
// repeatAsyncFn - Internally this uses Promise.all
it('use repeatAsyncFn', async () => {
// when you don't care about the return value
await repeatAsyncFn(10_000, async () => {
expect(true).toBe(true);
})
// when you care about the return value
const result = await repeatAsyncFn(10_000, async () => {
if (Math.random() > 0.5) return true;
return false;
});
expect(Array.isArray(result)).toBe(true);
result.forEach((item) => expect(item).oneOf([true, false]))
})
Dependencies
npmvitest
Auto Install
npx shadcn@latest add https://shadcn-registry-ts.vercel.app/r/util-vitest-utils.json
Manual Install
vitest.utils.ts
/**
* Source: http://localhost:3000
*/
/**
* Repaet a function a number of times. Sync version, that accept only a sync function.
*/
export function repeatSyncFn(
/** how many times to repeat */
times: number,
/** what function to repeat */
fn: () => void,
) {
for (let i = 0; i < times; i++) {
fn();
}
};
/**
* Repeat a function a number of times. Async version, that accept only a async function.
*/
export async function repeatAsyncFnInParallel<T>(
/** how many times to repeat */
times: number,
/** what function to repeat. */
fn: () => Promise<T>
) {
return Promise.all(
Array.from({ length: times }, fn)
);
}
Test
No test
Command Palette
Search for a command to run...