UNPKG

jest-date

Version:

Custom jest matchers to test dates

75 lines (71 loc) 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deriveWeekdayMessage = exports.deriveRelativeDateMessage = exports.checkDate = exports.NotADateError = void 0; const jest_matcher_utils_1 = require("jest-matcher-utils"); const date_fns_1 = require("date-fns"); class NotADateError extends Error { constructor(type, date, matcherFn, options = {}) { super(); /* istanbul ignore next */ if (Error.captureStackTrace) { Error.captureStackTrace(this, matcherFn); } this.message = [ (0, jest_matcher_utils_1.matcherHint)(`${options.invert ? '.not' : ''}.${matcherFn.name}`, 'received', options.excludeExpected ? '' : 'expected'), '', `${type === 'received' ? (0, jest_matcher_utils_1.RECEIVED_COLOR)('received') : (0, jest_matcher_utils_1.EXPECTED_COLOR)('expected')} value must be a valid Date.`, type === 'received' ? (0, jest_matcher_utils_1.printWithType)('Received', date, jest_matcher_utils_1.printReceived) : (0, jest_matcher_utils_1.printWithType)('Expected', date, jest_matcher_utils_1.printExpected), ].join('\n'); } } exports.NotADateError = NotADateError; function checkDate(type, date, matcher, options = {}) { if (date instanceof Date === false || !(0, date_fns_1.isValid)(date)) { throw new NotADateError(type, date, matcher, options); } } exports.checkDate = checkDate; function deriveRelativeDateMessage({ name, expected, received, invert, }) { const MESSAGE_TEMPLATE = `{matcher_hint} Expected date {received} {shakespeare} {expected_relation} {expected}, but it was {actual_relation}.`; return MESSAGE_TEMPLATE.replace('{matcher_hint}', (0, jest_matcher_utils_1.matcherHint)(`${invert ? '.not' : ''}.${name}`, expected.toISOString(), received.toISOString())) .replace('{received}', (0, jest_matcher_utils_1.printReceived)(received)) .replace('{shakespeare}', invert ? 'not to be' : 'to be') .replace('{expected_relation}', name .split(/(?=[A-Z])/) .slice(2) .map(s => s.toLowerCase()) .join(' ')) .replace('{expected}', (0, jest_matcher_utils_1.printExpected)(expected)) .replace('{actual_relation}', `${(0, date_fns_1.formatDistanceStrict)(expected, received)} ${(0, date_fns_1.isBefore)(received, expected) ? 'before' : 'after'}`); } exports.deriveRelativeDateMessage = deriveRelativeDateMessage; function deriveWeekdayMessage({ name, received, invert }) { const WEEKDAYS = [ 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', ]; const MESSAGE_TEMPLATE = `{matcher_hint} Expected date {received} to be on a {expected_day}, but it was on a {actual_day}.`; const INVERTED_MESSAGE_TEMPLATE = `{matcher_hint} Expected date {received} not to be on a {expected_day}, but it was.`; const template = invert ? INVERTED_MESSAGE_TEMPLATE : MESSAGE_TEMPLATE; return template .replace('{matcher_hint}', (0, jest_matcher_utils_1.matcherHint)(`${invert ? '.not' : ''}.${name}`, received.toISOString(), '')) .replace('{received}', (0, jest_matcher_utils_1.printReceived)(received)) .replace('{expected_day}', name .split(/(?=[A-Z])/) .slice(-1)[0] .toLowerCase()) .replace('{actual_day}', WEEKDAYS[(0, date_fns_1.getDay)(received)]); } exports.deriveWeekdayMessage = deriveWeekdayMessage;