fast-check
Version:
Property based testing framework for JavaScript (like QuickCheck)
45 lines (44 loc) • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AsyncProperty = void 0;
const PreconditionFailure_1 = require("../precondition/PreconditionFailure");
const IRawProperty_1 = require("./IRawProperty");
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(IRawProperty_1.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_1.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;
}
}
exports.AsyncProperty = AsyncProperty;
AsyncProperty.dummyHook = () => { };