UNPKG

@fast-check/vitest

Version:

Property based testing for Vitest based on fast-check

40 lines (39 loc) 1.7 kB
import { createTaskCollector, getCurrentSuite } from 'vitest/suite'; import { assert, asyncProperty, gen, readConfigureGlobal } from 'fast-check'; function isSig1OrSig2(args) { return typeof args[1] === 'function' || (args[1] === undefined && args[2] === undefined); } function taskCollectorBuilder(...args) { const [name, fn, options] = isSig1OrSig2(args) ? args : [args[0], args[2], args[1]]; const taskName = typeof name === 'function' ? name.name : name; getCurrentSuite().task(taskName, { ...this, ...(typeof options === 'number' ? { timeout: options } : options), handler: fn !== undefined ? async (context) => { let calledOnce = false; const config = readConfigureGlobal(); try { await assert(asyncProperty(gen(), (g) => { const refinedG = Object.assign((arb, ...args) => { calledOnce = true; return g(arb, ...args); }, { values: () => g.values() }); return fn({ ...context, g: refinedG }); }), { numRuns: config.numRuns ?? 1, endOnFailure: config.endOnFailure ?? true, includeErrorInReport: false, }); } catch (error) { if (calledOnce) { throw error; } throw error.cause; } } : undefined, }); } export const testAPIRefined = createTaskCollector(taskCollectorBuilder);