UNPKG

fast-check

Version:

Property based testing framework for JavaScript (like QuickCheck)

41 lines (40 loc) 1.36 kB
import { PreconditionFailure } from '../precondition/PreconditionFailure.js'; import { runIdToFrequency } from './IRawProperty.js'; export class AsyncProperty { constructor(arb, predicate) { this.arb = arb; this.predicate = predicate; this.beforeEachHook = AsyncProperty.dummyHook; this.afterEachHook = AsyncProperty.dummyHook; this.isAsync = () => true; } generate(mrng, runId) { return runId != null ? this.arb.withBias(runIdToFrequency(runId)).generate(mrng) : this.arb.generate(mrng); } async run(v) { await this.beforeEachHook(); try { const output = await 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 { await this.afterEachHook(); } } beforeEach(hookFunction) { this.beforeEachHook = hookFunction; return this; } afterEach(hookFunction) { this.afterEachHook = hookFunction; return this; } } AsyncProperty.dummyHook = () => { };