Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jest-matcher-utils

Package Overview
Dependencies
Maintainers
6
Versions
232
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-matcher-utils - npm Package Compare versions

Comparing version 25.0.0 to 25.1.0

build/deepCyclicCopyReplaceable.d.ts

37

build/index.d.ts

@@ -7,3 +7,4 @@ /**

*/
import { DiffOptions } from 'jest-diff';
import chalk = require('chalk');
import { DiffOptions as ImportDiffOptions } from 'jest-diff';
declare type MatcherHintColor = (arg: string) => string;

@@ -20,18 +21,8 @@ export declare type MatcherHintOptions = {

};
export { DiffOptions };
export declare const EXPECTED_COLOR: import("chalk").Chalk & {
supportsColor: import("chalk").ColorSupport;
};
export declare const RECEIVED_COLOR: import("chalk").Chalk & {
supportsColor: import("chalk").ColorSupport;
};
export declare const INVERTED_COLOR: import("chalk").Chalk & {
supportsColor: import("chalk").ColorSupport;
};
export declare const BOLD_WEIGHT: import("chalk").Chalk & {
supportsColor: import("chalk").ColorSupport;
};
export declare const DIM_COLOR: import("chalk").Chalk & {
supportsColor: import("chalk").ColorSupport;
};
export declare type DiffOptions = ImportDiffOptions;
export declare const EXPECTED_COLOR: chalk.Chalk;
export declare const RECEIVED_COLOR: chalk.Chalk;
export declare const INVERTED_COLOR: chalk.Chalk;
export declare const BOLD_WEIGHT: chalk.Chalk;
export declare const DIM_COLOR: chalk.Chalk;
export declare const SUGGEST_TO_CONTAIN_EQUAL: string;

@@ -44,4 +35,13 @@ export declare const stringify: (object: unknown, maxDepth?: number) => string;

export declare const ensureNoExpected: (expected: unknown, matcherName: string, options?: MatcherHintOptions | undefined) => void;
/**
* Ensures that `actual` is of type `number | bigint`
*/
export declare const ensureActualIsNumber: (actual: unknown, matcherName: string, options?: MatcherHintOptions | undefined) => void;
/**
* Ensures that `expected` is of type `number | bigint`
*/
export declare const ensureExpectedIsNumber: (expected: unknown, matcherName: string, options?: MatcherHintOptions | undefined) => void;
/**
* Ensures that `actual` & `expected` are of type `number | bigint`
*/
export declare const ensureNumbers: (actual: unknown, expected: unknown, matcherName: string, options?: MatcherHintOptions | undefined) => void;

@@ -54,4 +54,5 @@ export declare const ensureExpectedIsNonNegativeInteger: (expected: unknown, matcherName: string, options?: MatcherHintOptions | undefined) => void;

export declare const getLabelPrinter: (...strings: string[]) => PrintLabel;
export declare const matcherErrorMessage: (hint: string, generic: string, specific: string) => string;
export declare const matcherErrorMessage: (hint: string, generic: string, specific?: string | undefined) => string;
export declare const matcherHint: (matcherName: string, received?: string, expected?: string, options?: MatcherHintOptions) => string;
export {};
//# sourceMappingURL=index.d.ts.map

@@ -6,8 +6,2 @@ 'use strict';

});
Object.defineProperty(exports, 'DiffOptions', {
enumerable: true,
get: function() {
return _jestDiff.DiffOptions;
}
});
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,29 +13,52 @@

var _jestGetType = _interopRequireWildcard(require('jest-get-type'));
var _jestGetType = _interopRequireDefault(require('jest-get-type'));
var _prettyFormat = _interopRequireDefault(require('pretty-format'));
var _Replaceable = _interopRequireDefault(require('./Replaceable'));
var _deepCyclicCopyReplaceable = _interopRequireDefault(
require('./deepCyclicCopyReplaceable')
);
function _getRequireWildcardCache() {
if (typeof WeakMap !== 'function') return null;
var cache = new WeakMap();
_getRequireWildcardCache = function() {
return cache;
};
return cache;
}
function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = {};
if (obj != null) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc =
Object.defineProperty && Object.getOwnPropertyDescriptor
? Object.getOwnPropertyDescriptor(obj, key)
: {};
if (desc.get || desc.set) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
return {default: obj};
}
var cache = _getRequireWildcardCache();
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var newObj = {};
var hasPropertyDescriptor =
Object.defineProperty && Object.getOwnPropertyDescriptor;
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor
? Object.getOwnPropertyDescriptor(obj, key)
: null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
newObj.default = obj;
return newObj;
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
}

@@ -186,2 +203,5 @@

};
/**
* Ensures that `actual` is of type `number | bigint`
*/

@@ -191,3 +211,3 @@ exports.ensureNoExpected = ensureNoExpected;

const ensureActualIsNumber = (actual, matcherName, options) => {
if (typeof actual !== 'number') {
if (typeof actual !== 'number' && typeof actual !== 'bigint') {
// Prepend maybe not only for backward compatibility.

@@ -198,3 +218,3 @@ const matcherString = (options ? '' : '[.not]') + matcherName;

matcherHint(matcherString, undefined, undefined, options),
`${RECEIVED_COLOR('received')} value must be a number`,
`${RECEIVED_COLOR('received')} value must be a number or bigint`,
printWithType('Received', actual, printReceived)

@@ -205,2 +225,5 @@ )

};
/**
* Ensures that `expected` is of type `number | bigint`
*/

@@ -210,3 +233,3 @@ exports.ensureActualIsNumber = ensureActualIsNumber;

const ensureExpectedIsNumber = (expected, matcherName, options) => {
if (typeof expected !== 'number') {
if (typeof expected !== 'number' && typeof expected !== 'bigint') {
// Prepend maybe not only for backward compatibility.

@@ -217,3 +240,3 @@ const matcherString = (options ? '' : '[.not]') + matcherName;

matcherHint(matcherString, undefined, undefined, options),
`${EXPECTED_COLOR('expected')} value must be a number`,
`${EXPECTED_COLOR('expected')} value must be a number or bigint`,
printWithType('Expected', expected, printExpected)

@@ -224,2 +247,5 @@ )

};
/**
* Ensures that `actual` & `expected` are of type `number | bigint`
*/

@@ -251,6 +277,24 @@ exports.ensureExpectedIsNumber = ensureExpectedIsNumber;

}
};
}; // Given array of diffs, return concatenated string:
// * include common substrings
// * exclude change substrings which have opposite op
// * include change substrings which have argument op
// with inverse highlight only if there is a common substring
exports.ensureExpectedIsNonNegativeInteger = ensureExpectedIsNonNegativeInteger;
const getCommonAndChangedSubstrings = (diffs, op, hasCommonDiff) =>
diffs.reduce(
(reduced, diff) =>
reduced +
(diff[0] === _jestDiff.DIFF_EQUAL
? diff[1]
: diff[0] !== op
? ''
: hasCommonDiff
? INVERTED_COLOR(diff[1])
: diff[1]),
''
);
const isLineDiffable = (expected, received) => {

@@ -264,3 +308,3 @@ const expectedType = (0, _jestGetType.default)(expected);

if ((0, _jestGetType.isPrimitive)(expected)) {
if (_jestGetType.default.isPrimitive(expected)) {
// Print generic line diff for strings only:

@@ -307,2 +351,4 @@ // * if neither string is empty

const MAX_DIFF_STRING_LENGTH = 20000;
const printDiffOrStringify = (

@@ -315,27 +361,68 @@ expected,

) => {
if (typeof expected === 'string' && typeof received === 'string') {
const result = (0, _jestDiff.getStringDiff)(expected, received, {
aAnnotation: expectedLabel,
bAnnotation: receivedLabel,
expand
});
if (
typeof expected === 'string' &&
typeof received === 'string' &&
expected.length !== 0 &&
received.length !== 0 &&
expected.length <= MAX_DIFF_STRING_LENGTH &&
received.length <= MAX_DIFF_STRING_LENGTH &&
expected !== received
) {
if (expected.includes('\n') || received.includes('\n')) {
return (0, _jestDiff.diffStringsUnified)(expected, received, {
aAnnotation: expectedLabel,
bAnnotation: receivedLabel,
changeLineTrailingSpaceColor: _chalk.default.bgYellow,
commonLineTrailingSpaceColor: _chalk.default.bgYellow,
emptyFirstOrLastLinePlaceholder: '↵',
// U+21B5
expand,
includeChangeCounts: true
});
}
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;
}
const diffs = (0, _jestDiff.diffStringsRaw)(expected, received, true);
const hasCommonDiff = diffs.some(diff => diff[0] === _jestDiff.DIFF_EQUAL);
const printLabel = getLabelPrinter(expectedLabel, receivedLabel);
const expectedLine =
printLabel(expectedLabel) +
printExpected(
getCommonAndChangedSubstrings(
diffs,
_jestDiff.DIFF_DELETE,
hasCommonDiff
)
);
const receivedLine =
printLabel(receivedLabel) +
printReceived(
getCommonAndChangedSubstrings(
diffs,
_jestDiff.DIFF_INSERT,
hasCommonDiff
)
);
return expectedLine + '\n' + receivedLine;
}
if (isLineDiffable(expected, received)) {
const difference = (0, _jestDiff.default)(expected, received, {
aAnnotation: expectedLabel,
bAnnotation: receivedLabel,
expand
});
const {
replacedExpected,
replacedReceived
} = replaceMatchedToAsymmetricMatcher(
(0, _deepCyclicCopyReplaceable.default)(expected),
(0, _deepCyclicCopyReplaceable.default)(received),
[],
[]
);
const difference = (0, _jestDiff.default)(
replacedExpected,
replacedReceived,
{
aAnnotation: expectedLabel,
bAnnotation: receivedLabel,
expand,
includeChangeCounts: true
}
);

@@ -370,2 +457,6 @@ if (

if (typeof actual === 'bigint' && typeof expected === 'bigint') {
return false;
}
if (typeof actual === 'boolean' && typeof expected === 'boolean') {

@@ -378,2 +469,64 @@ return false;

function replaceMatchedToAsymmetricMatcher(
replacedExpected,
replacedReceived,
expectedCycles,
receivedCycles
) {
if (!_Replaceable.default.isReplaceable(replacedExpected, replacedReceived)) {
return {
replacedExpected,
replacedReceived
};
}
if (
expectedCycles.includes(replacedExpected) ||
receivedCycles.includes(replacedReceived)
) {
return {
replacedExpected,
replacedReceived
};
}
expectedCycles.push(replacedExpected);
receivedCycles.push(replacedReceived);
const expectedReplaceable = new _Replaceable.default(replacedExpected);
const receivedReplaceable = new _Replaceable.default(replacedReceived);
expectedReplaceable.forEach((expectedValue, key) => {
const receivedValue = receivedReplaceable.get(key);
if (isAsymmetricMatcher(expectedValue)) {
if (expectedValue.asymmetricMatch(receivedValue)) {
receivedReplaceable.set(key, expectedValue);
}
} else if (isAsymmetricMatcher(receivedValue)) {
if (receivedValue.asymmetricMatch(expectedValue)) {
expectedReplaceable.set(key, receivedValue);
}
} else if (
_Replaceable.default.isReplaceable(expectedValue, receivedValue)
) {
const replaced = replaceMatchedToAsymmetricMatcher(
expectedValue,
receivedValue,
expectedCycles,
receivedCycles
);
expectedReplaceable.set(key, replaced.replacedExpected);
receivedReplaceable.set(key, replaced.replacedReceived);
}
});
return {
replacedExpected: expectedReplaceable.object,
replacedReceived: receivedReplaceable.object
};
}
function isAsymmetricMatcher(data) {
const type = (0, _jestGetType.default)(data);
return type === 'object' && typeof data.asymmetricMatch === 'function';
}
const diff = (a, b, options) =>

@@ -407,5 +560,5 @@ shouldPrintDiff(a, b) ? (0, _jestDiff.default)(a, b, options) : null;

) =>
`${hint}\n\n${_chalk.default.bold(
'Matcher error'
)}: ${generic}\n\n${specific}`; // Display assertion for the report when a test fails.
`${hint}\n\n${_chalk.default.bold('Matcher error')}: ${generic}${
typeof specific === 'string' ? '\n\n' + specific : ''
}`; // Display assertion for the report when a test fails.
// New format: rejects/resolves, not, and matcher name have black color

@@ -412,0 +565,0 @@ // Old format: matcher name has dim color

{
"name": "jest-matcher-utils",
"description": "A set of utility functions for expect and related packages",
"version": "25.0.0",
"version": "25.1.0",
"repository": {

@@ -11,3 +11,3 @@ "type": "git",

"engines": {
"node": ">= 8"
"node": ">= 8.3"
},

@@ -17,6 +17,6 @@ "license": "MIT",

"dependencies": {
"chalk": "^2.0.1",
"jest-diff": "^25.0.0",
"jest-get-type": "^25.0.0",
"pretty-format": "^25.0.0"
"chalk": "^3.0.0",
"jest-diff": "^25.1.0",
"jest-get-type": "^25.1.0",
"pretty-format": "^25.1.0"
},

@@ -26,3 +26,3 @@ "publishConfig": {

},
"gitHead": "ff9269be05fd8316e95232198fce3463bf2f270e"
"gitHead": "170eee11d03b0ed5c60077982fdbc3bafd403638"
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc