@fast-check/vitest
Version:
Property based testing for Vitest based on fast-check
43 lines (42 loc) • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.testAPIRefined = void 0;
const suite_1 = require("vitest/suite");
const fast_check_1 = require("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;
(0, suite_1.getCurrentSuite)().task(taskName, {
...this,
...(typeof options === 'number' ? { timeout: options } : options),
handler: fn !== undefined
? async (context) => {
let calledOnce = false;
const config = (0, fast_check_1.readConfigureGlobal)();
try {
await (0, fast_check_1.assert)((0, fast_check_1.asyncProperty)((0, fast_check_1.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,
});
}
exports.testAPIRefined = (0, suite_1.createTaskCollector)(taskCollectorBuilder);