handle async checks
This commit is contained in:
parent
e2bd5b1d26
commit
9aea39be37
22
src/index.ts
22
src/index.ts
|
@ -1,6 +1,7 @@
|
|||
import { Arbitrary, check, Parameters, property, RunDetails } from 'fast-check';
|
||||
import { Arbitrary, asyncProperty, check, Parameters, property, RunDetails } from 'fast-check';
|
||||
|
||||
export type Check<T> = (this: Mocha.Context, val: T) => boolean | never | void;
|
||||
export type CheckStatus = boolean | void;
|
||||
export type Check<T> = (this: Mocha.Context, val: T) => never | CheckStatus | Promise<CheckStatus>;
|
||||
export type WrappedIt<T> = (name: string, check: Check<T>) => void;
|
||||
export type Suite<T> = (it: WrappedIt<T>) => void;
|
||||
|
||||
|
@ -9,14 +10,14 @@ export interface ErrorParameters<T> extends Parameters<T> {
|
|||
errorReporter?: ErrorReporter<T>;
|
||||
}
|
||||
|
||||
export function over<T>(name: string, strategy: Arbitrary<T>, suite: Suite<T>, parameters?: ErrorParameters<T>): void {
|
||||
export function over<T>(name: string, strategy: Arbitrary<T>, suite: Suite<T>, parameters: ErrorParameters<T> = {}): void {
|
||||
describe(name, () => {
|
||||
suite((name, test) => {
|
||||
it(name, function (this: Mocha.Context): Promise<void> {
|
||||
const ctx = this;
|
||||
// something about check's type signature requires examples to be tuples,
|
||||
// which leads to triple-wrapping examples for tuple properties. help remove one layer
|
||||
const examples: Array<[T]> = parameters?.examples?.map((it) => [it]) || [];
|
||||
const examples: Array<[T]> = parameters.examples?.map((it) => [it]) || [];
|
||||
const checkParameters: Parameters<[T]> = {
|
||||
...parameters,
|
||||
// handle result formatting here
|
||||
|
@ -24,15 +25,16 @@ export function over<T>(name: string, strategy: Arbitrary<T>, suite: Suite<T>, p
|
|||
reporter: undefined,
|
||||
examples,
|
||||
};
|
||||
const reporter = (parameters?.errorReporter || briefReporter) as ErrorReporter<[T]>;
|
||||
const reporter = (parameters.errorReporter || briefReporter) as ErrorReporter<[T]>;
|
||||
|
||||
return new Promise((res, rej) => {
|
||||
// wrap the strategy arb in a one-shot property checking the test fn
|
||||
const result = check(property(strategy, (val) => test.call(ctx, val)), checkParameters);
|
||||
// wrap the strategy arb in a one-shot property checking the test fn
|
||||
// TODO: switch between property and asyncProperty as needed
|
||||
const property = asyncProperty(strategy, (val) => Promise.resolve(test.call(ctx, val)));
|
||||
return Promise.resolve(check(property, checkParameters)).then((result) => {
|
||||
if (result.failed) {
|
||||
rej(new Error(reporter(result)));
|
||||
throw new Error(reporter(result));
|
||||
} else {
|
||||
res();
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { expect } from 'chai';
|
||||
import { array, defaultReportMessage, integer, lorem, oneof, tuple, uuid } from 'fast-check';
|
||||
import { array, defaultReportMessage, integer, lorem, tuple, uuid } from 'fast-check';
|
||||
|
||||
import { over } from '../src/index';
|
||||
import { over } from '../src';
|
||||
|
||||
const LARGE_VALUE = Math.floor(Math.random() * 1_000_000_000);
|
||||
|
||||
|
@ -22,6 +22,10 @@ describe('example properties', () => {
|
|||
|
||||
return true;
|
||||
});
|
||||
|
||||
it('should resolve async checks', async (n: number) => {
|
||||
expect(n).to.be.lessThanOrEqual(90);
|
||||
});
|
||||
});
|
||||
|
||||
over('some IDs', uuid(), (it) => {
|
||||
|
|
Loading…
Reference in New Issue