@fgv/ts-utils-jest
Version:
Custom matchers for ts-utils result class
51 lines • 2.01 kB
JavaScript
import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
import { matcherName, predicate } from './predicate';
import { matcherHint } from 'jest-matcher-utils';
function passMessage(received) {
const expected = 'successful callback';
const got = [printReceivedResult(received)];
got.push(' Callback returned true');
return () => [
matcherHint(`.not.${matcherName}`, 'result', 'callback'),
printExpectedResult('success', false, expected),
...got
].join('\n');
}
function failMessage(received, cbResult) {
const expected = 'successful callback';
const got = [printReceivedResult(received)];
/* c8 ignore else */
if (cbResult.isFailure()) {
got.push(cbResult.message);
}
else if (cbResult.value === false) {
got.push(' Callback returned false');
}
else if (cbResult.value === undefined) {
got.push(' Callback was not invoked');
}
else {
throw new Error('Internal error: toSucceedAndSatisfy.failMessage passed success with true');
}
return () => [
matcherHint(`${matcherName}`, 'result', 'callback'),
printExpectedResult('success', true, expected),
...got
].join('\n');
}
export default {
toSucceedAndSatisfy: function (received, test) {
// For the normal (not '.not') case, we do not want to capture exceptions
// so that the IDE can display exactly the line on which the failure case.
// For the .not case, we want to swallow exceptions or expect failures since
// we're just testing failure and not the reason.
const capture = this.isNot;
const cbResult = predicate(received, test, !!capture);
const pass = cbResult.isSuccess() && cbResult.value === true;
if (pass) {
return { pass: true, message: passMessage(received) };
}
return { pass: false, message: failMessage(received, cbResult) };
}
};
//# sourceMappingURL=index.js.map