UNPKG

@fgv/ts-utils-jest

Version:
69 lines 2.75 kB
import { getTypeOfProperty, getValueOfPropertyOrDefault } from '@fgv/ts-utils'; import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils'; import { matcherName, predicate } from './predicate'; function getRange(length, cursor) { // less than 3 return everything if (length < 3) { return { start: 0, end: length }; } if (cursor === 0) { return { start: 0, end: 3 }; } else if (cursor >= length - 1) { return { start: length - 3, end: length }; } return { start: cursor - 1, end: cursor + 2 }; } function formatOneCall(index, received, cursor) { const indexString = index.toLocaleString([], { maximumFractionDigits: 0, minimumIntegerDigits: 3 }); const cursorString = cursor ? '*' : ' '; return `${cursorString}${indexString}: ${printReceived(received)}`; } function formatArgsMessage(received, cursor) { const calls = received.mock.calls; if (calls.length > 0) { // if there's no cursor, show the last 3 const { start, end } = getRange(calls.length, cursor !== undefined ? cursor : calls.length - 1); const callsToShow = calls.slice(start, end); return callsToShow.map((c, i) => formatOneCall(start + i, c, start + i === cursor)); } return []; } function passMessage(received, expected, matched) { return () => [ matcherHint(`.not.${matcherName}`), 'Expected no call with arguments matching:', ` ${printExpected(expected)}`, `Received (${received.mock.calls.length} total):`, ...formatArgsMessage(received, matched.index) ].join('\n'); } function failMessage(received, expected) { return () => [ matcherHint(`${matcherName}`), 'Expected call with arguments matching:', ` ${printExpected(expected)}`, `Received (${received.mock.calls.length} total):`, ...formatArgsMessage(received) ].join('\n'); } function isMock(received) { if (received !== null && !Array.isArray(received)) { return (getValueOfPropertyOrDefault('_isMockFunction', received) === true && getTypeOfProperty('mock', received) === 'object'); } return false; } export default { toHaveBeenCalledWithArgumentsMatching: function (received, expected) { if (!isMock(received)) { throw new Error('Test error: toHaveBeenCalledWithArgumentsMatching called with other than jest.Mock'); } const matched = predicate(received, expected); if (matched !== undefined) { return { pass: true, message: passMessage(received, expected, matched) }; } return { pass: false, message: failMessage(received, expected) }; } }; //# sourceMappingURL=index.js.map