UNPKG

@fgv/ts-utils-jest

Version:

Custom matchers for ts-utils result class

53 lines 2.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const matcherHelpers_1 = require("../../utils/matcherHelpers"); const predicate_1 = require("./predicate"); const jest_matcher_utils_1 = require("jest-matcher-utils"); function passMessage(received) { const expected = 'successful callback'; const got = [(0, matcherHelpers_1.printReceivedResult)(received)]; got.push(' Callback returned true'); return () => [ (0, jest_matcher_utils_1.matcherHint)(`.not.${predicate_1.matcherName}`, 'result', 'callback'), (0, matcherHelpers_1.printExpectedResult)('success', false, expected), ...got ].join('\n'); } function failMessage(received, cbResult) { const expected = 'successful callback'; const got = [(0, matcherHelpers_1.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 () => [ (0, jest_matcher_utils_1.matcherHint)(`${predicate_1.matcherName}`, 'result', 'callback'), (0, matcherHelpers_1.printExpectedResult)('success', true, expected), ...got ].join('\n'); } exports.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 = (0, predicate_1.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