jest-matcher-utils
Advanced tools
Comparing version 24.8.0 to 24.9.0
@@ -7,9 +7,13 @@ /** | ||
*/ | ||
import jestDiff, { DiffOptions } from 'jest-diff'; | ||
import { DiffOptions } from 'jest-diff'; | ||
declare type MatcherHintColor = (arg: string) => string; | ||
export declare type MatcherHintOptions = { | ||
comment?: string; | ||
expectedColor?: MatcherHintColor; | ||
isDirectExpectCall?: boolean; | ||
isNot?: boolean; | ||
promise?: string; | ||
receivedColor?: MatcherHintColor; | ||
secondArgument?: string; | ||
secondArgumentColor?: MatcherHintColor; | ||
}; | ||
@@ -43,3 +47,4 @@ export { DiffOptions }; | ||
export declare const ensureExpectedIsNonNegativeInteger: (expected: unknown, matcherName: string, options?: MatcherHintOptions | undefined) => void; | ||
export declare const diff: typeof jestDiff; | ||
export declare const printDiffOrStringify: (expected: unknown, received: unknown, expectedLabel: string, receivedLabel: string, expand: boolean) => string; | ||
export declare const diff: (a: any, b: any, options?: import("jest-diff/build/types").DiffOptions | undefined) => string | null; | ||
export declare const pluralize: (word: string, count: number) => string; | ||
@@ -46,0 +51,0 @@ declare type PrintLabel = (string: string) => string; |
@@ -12,3 +12,3 @@ 'use strict'; | ||
}); | ||
exports.matcherHint = exports.matcherErrorMessage = exports.getLabelPrinter = exports.pluralize = exports.diff = exports.ensureExpectedIsNonNegativeInteger = exports.ensureNumbers = exports.ensureExpectedIsNumber = exports.ensureActualIsNumber = exports.ensureNoExpected = exports.printWithType = exports.printExpected = exports.printReceived = exports.highlightTrailingWhitespace = exports.stringify = exports.SUGGEST_TO_CONTAIN_EQUAL = exports.DIM_COLOR = exports.BOLD_WEIGHT = exports.INVERTED_COLOR = exports.RECEIVED_COLOR = exports.EXPECTED_COLOR = void 0; | ||
exports.matcherHint = exports.matcherErrorMessage = exports.getLabelPrinter = exports.pluralize = exports.diff = exports.printDiffOrStringify = exports.ensureExpectedIsNonNegativeInteger = exports.ensureNumbers = exports.ensureExpectedIsNumber = exports.ensureActualIsNumber = exports.ensureNoExpected = exports.printWithType = exports.printExpected = exports.printReceived = exports.highlightTrailingWhitespace = exports.stringify = exports.SUGGEST_TO_CONTAIN_EQUAL = exports.DIM_COLOR = exports.BOLD_WEIGHT = exports.INVERTED_COLOR = exports.RECEIVED_COLOR = exports.EXPECTED_COLOR = void 0; | ||
@@ -19,3 +19,3 @@ var _chalk = _interopRequireDefault(require('chalk')); | ||
var _jestGetType = _interopRequireDefault(require('jest-get-type')); | ||
var _jestGetType = _interopRequireWildcard(require('jest-get-type')); | ||
@@ -84,2 +84,5 @@ var _prettyFormat = _interopRequireDefault(require('pretty-format')); | ||
exports.DIM_COLOR = DIM_COLOR; | ||
const MULTILINE_REGEXP = /\n/; | ||
const SPACE_SYMBOL = '\u{00B7}'; // middle dot | ||
const NUMBERS = [ | ||
@@ -135,8 +138,12 @@ 'zero', | ||
const highlightTrailingWhitespace = text => | ||
text.replace(/\s+$/gm, _chalk.default.inverse('$&')); | ||
text.replace(/\s+$/gm, _chalk.default.inverse('$&')); // Instead of inverse highlight which now implies a change, | ||
// replace common spaces with middle dot at the end of any line. | ||
exports.highlightTrailingWhitespace = highlightTrailingWhitespace; | ||
const replaceTrailingSpaces = text => | ||
text.replace(/\s+$/gm, spaces => SPACE_SYMBOL.repeat(spaces.length)); | ||
const printReceived = object => | ||
RECEIVED_COLOR(highlightTrailingWhitespace(stringify(object))); | ||
RECEIVED_COLOR(replaceTrailingSpaces(stringify(object))); | ||
@@ -146,3 +153,3 @@ exports.printReceived = printReceived; | ||
const printExpected = value => | ||
EXPECTED_COLOR(highlightTrailingWhitespace(stringify(value))); | ||
EXPECTED_COLOR(replaceTrailingSpaces(stringify(value))); | ||
@@ -238,2 +245,105 @@ exports.printExpected = printExpected; | ||
} | ||
}; | ||
exports.ensureExpectedIsNonNegativeInteger = ensureExpectedIsNonNegativeInteger; | ||
const isLineDiffable = (expected, received) => { | ||
const expectedType = (0, _jestGetType.default)(expected); | ||
const receivedType = (0, _jestGetType.default)(received); | ||
if (expectedType !== receivedType) { | ||
return false; | ||
} | ||
if ((0, _jestGetType.isPrimitive)(expected)) { | ||
// Print generic line diff for strings only: | ||
// * if neither string is empty | ||
return ( | ||
typeof expected === 'string' && | ||
typeof received === 'string' && | ||
expected.length !== 0 && | ||
received.length !== 0 && | ||
(MULTILINE_REGEXP.test(expected) || MULTILINE_REGEXP.test(received)) | ||
); | ||
} | ||
if ( | ||
expectedType === 'date' || | ||
expectedType === 'function' || | ||
expectedType === 'regexp' | ||
) { | ||
return false; | ||
} | ||
if (expected instanceof Error && received instanceof Error) { | ||
return false; | ||
} | ||
if ( | ||
expectedType === 'object' && | ||
typeof expected.asymmetricMatch === 'function' | ||
) { | ||
return false; | ||
} | ||
if ( | ||
receivedType === 'object' && | ||
typeof received.asymmetricMatch === 'function' | ||
) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
const printDiffOrStringify = ( | ||
expected, | ||
received, | ||
expectedLabel, | ||
receivedLabel, | ||
expand | ||
) => { | ||
if (typeof expected === 'string' && typeof received === 'string') { | ||
const result = (0, _jestDiff.getStringDiff)(expected, received, { | ||
aAnnotation: expectedLabel, | ||
bAnnotation: receivedLabel, | ||
expand | ||
}); | ||
if (result !== null) { | ||
if (result.isMultiline) { | ||
return result.annotatedDiff; | ||
} | ||
const printLabel = getLabelPrinter(expectedLabel, receivedLabel); | ||
const expectedLine = printLabel(expectedLabel) + printExpected(result.a); | ||
const receivedLine = printLabel(receivedLabel) + printReceived(result.b); | ||
return expectedLine + '\n' + receivedLine; | ||
} | ||
} | ||
if (isLineDiffable(expected, received)) { | ||
const difference = (0, _jestDiff.default)(expected, received, { | ||
aAnnotation: expectedLabel, | ||
bAnnotation: receivedLabel, | ||
expand | ||
}); | ||
if ( | ||
typeof difference === 'string' && | ||
difference.includes('- ' + expectedLabel) && | ||
difference.includes('+ ' + receivedLabel) | ||
) { | ||
return difference; | ||
} | ||
} | ||
const printLabel = getLabelPrinter(expectedLabel, receivedLabel); | ||
const expectedLine = printLabel(expectedLabel) + printExpected(expected); | ||
const receivedLine = | ||
printLabel(receivedLabel) + | ||
(stringify(expected) === stringify(received) | ||
? 'serializes to the same string' | ||
: printReceived(received)); | ||
return expectedLine + '\n' + receivedLine; | ||
}; // Sometimes, e.g. when comparing two numbers, the output from jest-diff | ||
@@ -243,3 +353,3 @@ // does not contain more information than the `Expected:` / `Received:` already gives. | ||
exports.ensureExpectedIsNonNegativeInteger = ensureExpectedIsNonNegativeInteger; | ||
exports.printDiffOrStringify = printDiffOrStringify; | ||
@@ -302,2 +412,5 @@ const shouldPrintDiff = (actual, expected) => { | ||
comment = _options$comment === void 0 ? '' : _options$comment, | ||
_options$expectedColo = options.expectedColor, | ||
expectedColor = | ||
_options$expectedColo === void 0 ? EXPECTED_COLOR : _options$expectedColo, | ||
_options$isDirectExpe = options.isDirectExpectCall, | ||
@@ -310,5 +423,13 @@ isDirectExpectCall = | ||
promise = _options$promise === void 0 ? '' : _options$promise, | ||
_options$receivedColo = options.receivedColor, | ||
receivedColor = | ||
_options$receivedColo === void 0 ? RECEIVED_COLOR : _options$receivedColo, | ||
_options$secondArgume = options.secondArgument, | ||
secondArgument = | ||
_options$secondArgume === void 0 ? '' : _options$secondArgume; | ||
_options$secondArgume === void 0 ? '' : _options$secondArgume, | ||
_options$secondArgume2 = options.secondArgumentColor, | ||
secondArgumentColor = | ||
_options$secondArgume2 === void 0 | ||
? EXPECTED_COLOR | ||
: _options$secondArgume2; | ||
let hint = ''; | ||
@@ -318,3 +439,3 @@ let dimString = 'expect'; // concatenate adjacent dim substrings | ||
if (!isDirectExpectCall && received !== '') { | ||
hint += DIM_COLOR(dimString + '(') + RECEIVED_COLOR(received); | ||
hint += DIM_COLOR(dimString + '(') + receivedColor(received); | ||
dimString = ')'; | ||
@@ -346,6 +467,6 @@ } | ||
} else { | ||
hint += DIM_COLOR(dimString + '(') + EXPECTED_COLOR(expected); | ||
hint += DIM_COLOR(dimString + '(') + expectedColor(expected); | ||
if (secondArgument) { | ||
hint += DIM_COLOR(', ') + EXPECTED_COLOR(secondArgument); | ||
hint += DIM_COLOR(', ') + secondArgumentColor(secondArgument); | ||
} | ||
@@ -352,0 +473,0 @@ |
{ | ||
"name": "jest-matcher-utils", | ||
"description": "A set of utility functions for expect and related packages", | ||
"version": "24.8.0", | ||
"version": "24.9.0", | ||
"repository": { | ||
@@ -17,5 +17,5 @@ "type": "git", | ||
"chalk": "^2.0.1", | ||
"jest-diff": "^24.8.0", | ||
"jest-get-type": "^24.8.0", | ||
"pretty-format": "^24.8.0" | ||
"jest-diff": "^24.9.0", | ||
"jest-get-type": "^24.9.0", | ||
"pretty-format": "^24.9.0" | ||
}, | ||
@@ -25,3 +25,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "845728f24b3ef41e450595c384e9b5c9fdf248a4" | ||
"gitHead": "9ad0f4bc6b8bdd94989804226c28c9960d9da7d1" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
459
20441
5
Updatedjest-diff@^24.9.0
Updatedjest-get-type@^24.9.0
Updatedpretty-format@^24.9.0