UNPKG

fast-check

Version:

Property based testing framework for JavaScript (like QuickCheck)

41 lines (40 loc) 1.31 kB
import { PreconditionFailure } from '../precondition/PreconditionFailure.js'; import { runIdToFrequency } from './IRawProperty.js'; export class Property { constructor(arb, predicate) { this.arb = arb; this.predicate = predicate; this.beforeEachHook = Property.dummyHook; this.afterEachHook = Property.dummyHook; this.isAsync = () => false; } generate(mrng, runId) { return runId != null ? this.arb.withBias(runIdToFrequency(runId)).generate(mrng) : this.arb.generate(mrng); } run(v) { this.beforeEachHook(); try { const output = this.predicate(v); return output == null || output === true ? null : 'Property failed by returning false'; } catch (err) { if (PreconditionFailure.isFailure(err)) return err; if (err instanceof Error && err.stack) return `${err}\n\nStack trace: ${err.stack}`; return `${err}`; } finally { this.afterEachHook(); } } beforeEach(hookFunction) { this.beforeEachHook = hookFunction; return this; } afterEach(hookFunction) { this.afterEachHook = hookFunction; return this; } } Property.dummyHook = () => { };