UNPKG

@fast-check/ava

Version:

Property based testing for AVA based on fast-check

60 lines (59 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.testProp = exports.test = exports.fc = void 0; const ava_1 = require("ava"); exports.test = ava_1.default; const fc = require("fast-check"); exports.fc = fc; function wrapProp(arbitraries, prop, params) { return async (t, ..._args) => { let failingTry; try { await fc.assert(fc.asyncProperty(...arbitraries, async (...args) => { const tryResult = await t.try((tt) => prop(tt, ...args)); if (tryResult.passed) { tryResult.commit(); return true; } const encounteredPreconditionFailure = tryResult.errors.some((error) => fc.PreconditionFailure.isFailure(error.savedError) || fc.PreconditionFailure.isFailure(error.cause)); if (encounteredPreconditionFailure) { tryResult.discard(); fc.pre(false); } failingTry = tryResult; return false; }), params); } catch (error) { t.log(error.message); (failingTry?.commit ?? t.fail)(); } t.pass(); }; } function internalTestProp(testFn, label, arbitraries, prop, params) { const customParams = { ...params }; if (customParams.seed === undefined) { const seedFromGlobals = fc.readConfigureGlobal().seed; if (seedFromGlobals !== undefined) { customParams.seed = seedFromGlobals; } else { customParams.seed = Date.now() ^ (Math.random() * 0x100000000); } } testFn(`${label} (with seed=${customParams.seed})`, wrapProp(arbitraries, prop, customParams)); } function exposeModifier(modifier) { return (label, arbitraries, prop, params) => internalTestProp(ava_1.default[modifier], label, arbitraries, prop, params); } exports.testProp = Object.assign(function testProp(label, arbitraries, prop, params) { internalTestProp(ava_1.default, label, arbitraries, prop, params); }, { only: exposeModifier('only'), failing: exposeModifier('failing'), skip: exposeModifier('skip'), serial: exposeModifier('serial'), before: ava_1.default.before, after: ava_1.default.after, });