@fgv/ts-utils-jest
Version:
Custom matchers for ts-utils result class
17 lines • 892 B
JavaScript
import { captureResult, fail, succeed } from '../../ts-utils';
export const matcherName = 'toSucceedAndSatisfy';
// If the result is successful and callback does not throw, returns Success with the callback return value, or
// true if the callback returns undefined
// If the result is successful but the callback throws, returns Failure with the thrown error
// If the result is failure, returns Success with undefined
export function predicate(received, cb, capture) {
if (received.isSuccess()) {
const cbResult = capture ? captureResult(() => cb(received.value)) : succeed(cb(received.value));
if (cbResult.isSuccess()) {
return succeed(cbResult.value === true || cbResult.value === undefined);
}
return fail(` Callback failed with:\n ${cbResult.message}`);
}
return succeed(undefined);
}
//# sourceMappingURL=predicate.js.map