jest-matcher-utils
Advanced tools
Comparing version 24.0.0-alpha.12 to 24.0.0-alpha.13
@@ -6,6 +6,8 @@ 'use strict'; | ||
}); | ||
exports.matcherHint = exports.matcherErrorMessage = exports.getLabelPrinter = exports.pluralize = exports.ensureNumbers = exports.ensureExpectedIsNumber = exports.ensureActualIsNumber = exports.ensureNoExpected = exports.printWithType = exports.printExpected = exports.printReceived = exports.highlightTrailingWhitespace = exports.stringify = exports.SUGGEST_TO_CONTAIN_EQUAL = exports.SUGGEST_TO_EQUAL = exports.RECEIVED_COLOR = exports.EXPECTED_COLOR = void 0; | ||
exports.matcherHint = exports.matcherErrorMessage = exports.getLabelPrinter = exports.pluralize = exports.diff = exports.ensureNumbers = exports.ensureExpectedIsNumber = exports.ensureActualIsNumber = exports.ensureNoExpected = exports.printWithType = exports.printExpected = exports.printReceived = exports.highlightTrailingWhitespace = exports.stringify = exports.SUGGEST_TO_CONTAIN_EQUAL = exports.SUGGEST_TO_EQUAL = exports.RECEIVED_COLOR = exports.EXPECTED_COLOR = void 0; | ||
var _chalk = _interopRequireDefault(require('chalk')); | ||
var _jestDiff = _interopRequireDefault(require('jest-diff')); | ||
var _jestGetType = _interopRequireDefault(require('jest-get-type')); | ||
@@ -46,2 +48,3 @@ | ||
exports.RECEIVED_COLOR = RECEIVED_COLOR; | ||
const DIM_COLOR = _chalk.default.dim; | ||
const NUMBERS = [ | ||
@@ -133,10 +136,11 @@ 'zero', | ||
const ensureNoExpected = (expected, matcherName) => { | ||
matcherName || (matcherName = 'This'); | ||
const ensureNoExpected = (expected, matcherName, options) => { | ||
if (typeof expected !== 'undefined') { | ||
// Prepend maybe not only for backward compatibility. | ||
const matcherString = (options ? '' : '[.not]') + matcherName; | ||
throw new Error( | ||
matcherErrorMessage( | ||
matcherHint('[.not]' + matcherName, undefined, ''), | ||
`${EXPECTED_COLOR('expected')} value must be omitted or undefined`, | ||
matcherHint(matcherString, undefined, '', options), // Because expected is omitted in hint above, | ||
// expected is black instead of green in message below. | ||
'this matcher must not have an expected argument', | ||
printWithType('Expected', expected, printExpected) | ||
@@ -185,6 +189,25 @@ ) | ||
ensureExpectedIsNumber(expected, matcherName); | ||
}; | ||
}; // Sometimes, e.g. when comparing two numbers, the output from jest-diff | ||
// does not contain more information than the `Expected:` / `Received:` already gives. | ||
// In those cases, we do not print a diff to make the output shorter and not redundant. | ||
exports.ensureNumbers = ensureNumbers; | ||
const shouldPrintDiff = (actual, expected) => { | ||
if (typeof actual === 'number' && typeof expected === 'number') { | ||
return false; | ||
} | ||
if (typeof actual === 'boolean' && typeof expected === 'boolean') { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
const diff = (a, b, options) => | ||
shouldPrintDiff(a, b) ? (0, _jestDiff.default)(a, b, options) : null; | ||
exports.diff = diff; | ||
const pluralize = (word, count) => | ||
@@ -215,3 +238,5 @@ (NUMBERS[count] || count) + ' ' + word + (count === 1 ? '' : 's'); // To display lines of labeled values as two columns with monospace alignment: | ||
'Matcher error' | ||
)}: ${generic}\n\n${specific}`; | ||
)}: ${generic}\n\n${specific}`; // Display assertion for the report when a test fails. | ||
// New format: rejects/resolves, not, and matcher name have black color | ||
// Old format: matcher name has dim color | ||
@@ -226,22 +251,65 @@ exports.matcherErrorMessage = matcherErrorMessage; | ||
) => { | ||
const comment = options.comment, | ||
isDirectExpectCall = options.isDirectExpectCall, | ||
isNot = options.isNot, | ||
secondArgument = options.secondArgument; | ||
return ( | ||
_chalk.default.dim('expect' + (isDirectExpectCall ? '' : '(')) + | ||
RECEIVED_COLOR(received) + | ||
(isNot | ||
? `${_chalk.default.dim(').')}not${_chalk.default.dim(matcherName + '(')}` | ||
: _chalk.default.dim( | ||
(isDirectExpectCall ? '' : ')') + matcherName + '(' | ||
)) + | ||
EXPECTED_COLOR(expected) + | ||
(secondArgument | ||
? `${_chalk.default.dim(', ')}${EXPECTED_COLOR(secondArgument)}` | ||
: '') + | ||
_chalk.default.dim(`)${comment ? ` // ${comment}` : ''}`) | ||
); | ||
const _options$comment = options.comment, | ||
comment = _options$comment === void 0 ? '' : _options$comment, | ||
_options$isDirectExpe = options.isDirectExpectCall, | ||
isDirectExpectCall = | ||
_options$isDirectExpe === void 0 ? false : _options$isDirectExpe, | ||
_options$isNot = options.isNot, | ||
isNot = _options$isNot === void 0 ? false : _options$isNot, | ||
_options$promise = options.promise, | ||
promise = _options$promise === void 0 ? '' : _options$promise, | ||
_options$secondArgume = options.secondArgument, | ||
secondArgument = | ||
_options$secondArgume === void 0 ? '' : _options$secondArgume; | ||
let hint = ''; | ||
let dimString = 'expect'; // concatenate adjacent dim substrings | ||
if (!isDirectExpectCall && received !== '') { | ||
hint += DIM_COLOR(dimString + '(') + RECEIVED_COLOR(received); | ||
dimString = ')'; | ||
} | ||
if (promise !== '') { | ||
hint += DIM_COLOR(dimString + '.') + promise; | ||
dimString = ''; | ||
} | ||
if (isNot) { | ||
hint += DIM_COLOR(dimString + '.') + 'not'; | ||
dimString = ''; | ||
} | ||
if (matcherName.includes('.')) { | ||
// Old format: for backward compatibility, | ||
// especially without promise or isNot options | ||
dimString += matcherName; | ||
} else { | ||
// New format: omit period from matcherName arg | ||
hint += DIM_COLOR(dimString + '.') + matcherName; | ||
dimString = ''; | ||
} | ||
if (expected === '') { | ||
dimString += '()'; | ||
} else { | ||
hint += DIM_COLOR(dimString + '(') + EXPECTED_COLOR(expected); | ||
if (secondArgument) { | ||
hint += DIM_COLOR(', ') + EXPECTED_COLOR(secondArgument); | ||
} | ||
dimString = ')'; | ||
} | ||
if (comment !== '') { | ||
dimString += ' // ' + comment; | ||
} | ||
if (dimString !== '') { | ||
hint += DIM_COLOR(dimString); | ||
} | ||
return hint; | ||
}; | ||
exports.matcherHint = matcherHint; |
{ | ||
"name": "jest-matcher-utils", | ||
"description": "A set of utility functions for expect and related packages", | ||
"version": "24.0.0-alpha.12", | ||
"version": "24.0.0-alpha.13", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/facebook/jest.git" | ||
"url": "https://github.com/facebook/jest.git", | ||
"directory": "packages/jest-matcher-utils" | ||
}, | ||
@@ -16,6 +17,7 @@ "engines": { | ||
"chalk": "^2.0.1", | ||
"jest-get-type": "^24.0.0-alpha.12", | ||
"pretty-format": "^24.0.0-alpha.12" | ||
"jest-diff": "^24.0.0-alpha.13", | ||
"jest-get-type": "^24.0.0-alpha.13", | ||
"pretty-format": "^24.0.0-alpha.13" | ||
}, | ||
"gitHead": "4f2bcb861d1f0fb150c05970362e52a38c31f67e" | ||
"gitHead": "6de22dde9a10f775adc7b6f80080bdd224f6ae31" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10898
254
4
+ Addedjest-diff@^24.0.0-alpha.13
+ Addeddiff-sequences@24.9.0(transitive)
+ Addedjest-diff@24.9.0(transitive)