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

jest-diff

Package Overview
Dependencies
Maintainers
5
Versions
230
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-diff - npm Package Compare versions

Comparing version 23.0.0-alpha.2 to 23.0.0-alpha.4

29

build/constants.js
'use strict';
Object.defineProperty(exports, "__esModule", {
Object.defineProperty(exports, '__esModule', {
value: true

@@ -12,13 +12,20 @@ });

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
const NO_DIFF_MESSAGE = exports.NO_DIFF_MESSAGE = _chalk2.default.dim('Compared values have no visual difference.'); /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
*/
const NO_DIFF_MESSAGE = (exports.NO_DIFF_MESSAGE = _chalk2.default.dim(
'Compared values have no visual difference.'
)); /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
*/
const SIMILAR_MESSAGE = exports.SIMILAR_MESSAGE = _chalk2.default.dim('Compared values serialize to the same structure.\n' + 'Printing internal object structure without calling `toJSON` instead.');
const SIMILAR_MESSAGE = (exports.SIMILAR_MESSAGE = _chalk2.default.dim(
'Compared values serialize to the same structure.\n' +
'Printing internal object structure without calling `toJSON` instead.'
));
'use strict';
Object.defineProperty(exports, "__esModule", {
Object.defineProperty(exports, '__esModule', {
value: true

@@ -16,3 +16,5 @@ });

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}

@@ -30,10 +32,10 @@ const DIFF_CONTEXT_DEFAULT = 5; // removed | added | equal

*
*
*
*/
// Given chunk, return diff character.
const getDiffChar = chunk => chunk.removed ? '-' : chunk.added ? '+' : ' ';
const getDiffChar = chunk => (chunk.removed ? '-' : chunk.added ? '+' : ' ');
// Given diff character in line of hunk or computed from properties of chunk.
const getDiffDigit = char => char === '-' ? -1 : char === '+' ? 1 : 0;
const getDiffDigit = char => (char === '-' ? -1 : char === '+' ? 1 : 0);

@@ -54,15 +56,30 @@ // Color for text of line.

// Background color for leading or trailing spaces.
const getBgColor = (digit, onlyIndentationChanged) => digit === 0 && !onlyIndentationChanged ? _chalk2.default.bgYellow : _chalk2.default.inverse;
const getBgColor = (digit, onlyIndentationChanged) =>
digit === 0 && !onlyIndentationChanged
? _chalk2.default.bgYellow
: _chalk2.default.inverse;
// ONLY trailing if expected value is snapshot or multiline string.
const highlightTrailingSpaces = (line, bgColor) => line.replace(/\s+$/, bgColor('$&'));
const highlightTrailingSpaces = (line, bgColor) =>
line.replace(/\s+$/, bgColor('$&'));
// BOTH leading AND trailing if expected value is data structure.
const highlightLeadingTrailingSpaces = (line, bgColor
// If line consists of ALL spaces: highlight all of them.
) => highlightTrailingSpaces(line, bgColor).replace(
// If line has an ODD length of leading spaces: highlight only the LAST.
/^(\s\s)*(\s)(?=[^\s])/, '$1' + bgColor('$2'));
const highlightLeadingTrailingSpaces = (
line,
bgColor
// If line consists of ALL spaces: highlight all of them.
) =>
highlightTrailingSpaces(line, bgColor).replace(
// If line has an ODD length of leading spaces: highlight only the LAST.
/^(\s\s)*(\s)(?=[^\s])/,
'$1' + bgColor('$2')
);
const getAnnotation = options => _chalk2.default.green('- ' + (options && options.aAnnotation || 'Expected')) + '\n' + _chalk2.default.red('+ ' + (options && options.bAnnotation || 'Received')) + '\n\n';
const getAnnotation = options =>
_chalk2.default.green(
'- ' + ((options && options.aAnnotation) || 'Expected')
) +
'\n' +
_chalk2.default.red('+ ' + ((options && options.bAnnotation) || 'Received')) +
'\n\n';

@@ -88,11 +105,21 @@ // Given string, return array of its lines.

const lineOriginal = lineArray[0];
const onlyIndentationChanged = digit === 0 && lineOriginal.length !== lineArray[1].length;
const onlyIndentationChanged =
digit === 0 && lineOriginal.length !== lineArray[1].length;
return getColor(digit, onlyIndentationChanged)(char + ' ' +
// Prepend indentation spaces from original to compared line.
lineOriginal.slice(0, lineOriginal.length - lineCompared.length) + highlightLeadingTrailingSpaces(lineCompared, getBgColor(digit, onlyIndentationChanged)));
return getColor(digit, onlyIndentationChanged)(
char +
' ' +
// Prepend indentation spaces from original to compared line.
lineOriginal.slice(0, lineOriginal.length - lineCompared.length) +
highlightLeadingTrailingSpaces(
lineCompared,
getBgColor(digit, onlyIndentationChanged)
)
);
}
// Format compared line when expected is snapshot or multiline string.
return getColor(digit)(char + ' ' + highlightTrailingSpaces(lineCompared, getBgColor(digit)));
return getColor(digit)(
char + ' ' + highlightTrailingSpaces(lineCompared, getBgColor(digit))
);
};

@@ -129,11 +156,13 @@

const getOriginal = original && getterForChunks(original);
return chunks.reduce((lines, chunk) => {
const char = getDiffChar(chunk);
return chunks
.reduce((lines, chunk) => {
const char = getDiffChar(chunk);
splitIntoLines(chunk.value).forEach(line => {
lines.push(formatLine(char, line, getOriginal));
});
splitIntoLines(chunk.value).forEach(line => {
lines.push(formatLine(char, line, getOriginal));
});
return lines;
}, []).join('\n');
return lines;
}, [])
.join('\n');
};

@@ -146,3 +175,4 @@

// we can be sure that at least 1 line has been "hidden".
const shouldShowPatchMarks = (hunk, oldLinesCount) => oldLinesCount > hunk.oldLines;
const shouldShowPatchMarks = (hunk, oldLinesCount) =>
oldLinesCount > hunk.oldLines;

@@ -176,6 +206,17 @@ const createPatchMark = hunk => {

const options = {
context: typeof contextLines === 'number' && contextLines >= 0 ? contextLines : DIFF_CONTEXT_DEFAULT
context:
typeof contextLines === 'number' && contextLines >= 0
? contextLines
: DIFF_CONTEXT_DEFAULT
};
var _structuredPatch = (0, _diff.structuredPatch)('', '', a, b, '', '', options);
var _structuredPatch = (0, _diff.structuredPatch)(
'',
'',
a,
b,
'',
'',
options
);

@@ -190,15 +231,18 @@ const hunks = _structuredPatch.hunks;

const oldLinesCount = (a.match(/\n/g) || []).length;
return hunks.reduce((lines, hunk) => {
if (shouldShowPatchMarks(hunk, oldLinesCount)) {
lines.push(createPatchMark(hunk));
}
return hunks
.reduce((lines, hunk) => {
if (shouldShowPatchMarks(hunk, oldLinesCount)) {
lines.push(createPatchMark(hunk));
}
// Hunk properties are one-based but index args are zero-based.
const getOriginal = getter && getter(hunk.oldStart - 1, hunk.newStart - 1);
hunk.lines.forEach(line => {
lines.push(formatLine(line[0], line.slice(1), getOriginal));
});
// Hunk properties are one-based but index args are zero-based.
const getOriginal =
getter && getter(hunk.oldStart - 1, hunk.newStart - 1);
hunk.lines.forEach(line => {
lines.push(formatLine(line[0], line.slice(1), getOriginal));
});
return lines;
}, []).join('\n');
return lines;
}, [])
.join('\n');
};

@@ -216,5 +260,10 @@

// whenever linebreaks are involved.
const result = options && options.expand === false ? formatHunks(a, b, options && options.contextLines, original) : formatChunks(a, b, original);
const result =
options && options.expand === false
? formatHunks(a, b, options && options.contextLines, original)
: formatChunks(a, b, original);
return result === null ? _constants.NO_DIFF_MESSAGE : getAnnotation(options) + result;
}
return result === null
? _constants.NO_DIFF_MESSAGE
: getAnnotation(options) + result;
}

@@ -21,3 +21,5 @@ 'use strict';

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}

@@ -30,3 +32,3 @@ /**

*
*
*
*/

@@ -36,10 +38,16 @@

const AsymmetricMatcher = _prettyFormat$plugins.AsymmetricMatcher,
DOMCollection = _prettyFormat$plugins.DOMCollection,
DOMElement = _prettyFormat$plugins.DOMElement,
Immutable = _prettyFormat$plugins.Immutable,
ReactElement = _prettyFormat$plugins.ReactElement,
ReactTestComponent = _prettyFormat$plugins.ReactTestComponent;
DOMCollection = _prettyFormat$plugins.DOMCollection,
DOMElement = _prettyFormat$plugins.DOMElement,
Immutable = _prettyFormat$plugins.Immutable,
ReactElement = _prettyFormat$plugins.ReactElement,
ReactTestComponent = _prettyFormat$plugins.ReactTestComponent;
const PLUGINS = [ReactTestComponent, ReactElement, DOMElement, DOMCollection, Immutable, AsymmetricMatcher];
const PLUGINS = [
ReactTestComponent,
ReactElement,
DOMElement,
DOMCollection,
Immutable,
AsymmetricMatcher
];
const FORMAT_OPTIONS = {

@@ -88,3 +96,7 @@ plugins: PLUGINS

if (expectedType !== (0, _jestGetType2.default)(b)) {
return ' Comparing two different types of values.' + ` Expected ${_chalk2.default.green(expectedType)} but ` + `received ${_chalk2.default.red((0, _jestGetType2.default)(b))}.`;
return (
' Comparing two different types of values.' +
` Expected ${_chalk2.default.green(expectedType)} but ` +
`received ${_chalk2.default.red((0, _jestGetType2.default)(b))}.`
);
}

@@ -128,6 +140,11 @@

try {
diffMessage = (0, _diff_strings2.default)((0, _prettyFormat2.default)(a, FORMAT_OPTIONS_0), (0, _prettyFormat2.default)(b, FORMAT_OPTIONS_0), options, {
a: (0, _prettyFormat2.default)(a, FORMAT_OPTIONS),
b: (0, _prettyFormat2.default)(b, FORMAT_OPTIONS)
});
diffMessage = (0, _diff_strings2.default)(
(0, _prettyFormat2.default)(a, FORMAT_OPTIONS_0),
(0, _prettyFormat2.default)(b, FORMAT_OPTIONS_0),
options,
{
a: (0, _prettyFormat2.default)(a, FORMAT_OPTIONS),
b: (0, _prettyFormat2.default)(b, FORMAT_OPTIONS)
}
);
} catch (e) {

@@ -140,6 +157,11 @@ hasThrown = true;

if (!diffMessage || diffMessage === _constants.NO_DIFF_MESSAGE) {
diffMessage = (0, _diff_strings2.default)((0, _prettyFormat2.default)(a, FALLBACK_FORMAT_OPTIONS_0), (0, _prettyFormat2.default)(b, FALLBACK_FORMAT_OPTIONS_0), options, {
a: (0, _prettyFormat2.default)(a, FALLBACK_FORMAT_OPTIONS),
b: (0, _prettyFormat2.default)(b, FALLBACK_FORMAT_OPTIONS)
});
diffMessage = (0, _diff_strings2.default)(
(0, _prettyFormat2.default)(a, FALLBACK_FORMAT_OPTIONS_0),
(0, _prettyFormat2.default)(b, FALLBACK_FORMAT_OPTIONS_0),
options,
{
a: (0, _prettyFormat2.default)(a, FALLBACK_FORMAT_OPTIONS),
b: (0, _prettyFormat2.default)(b, FALLBACK_FORMAT_OPTIONS)
}
);
if (diffMessage !== _constants.NO_DIFF_MESSAGE && !hasThrown) {

@@ -153,2 +175,2 @@ diffMessage = _constants.SIMILAR_MESSAGE + '\n\n' + diffMessage;

module.exports = diff;
module.exports = diff;
{
"name": "jest-diff",
"version": "23.0.0-alpha.2",
"version": "23.0.0-alpha.4",
"repository": {

@@ -14,4 +14,4 @@ "type": "git",

"jest-get-type": "^22.1.0",
"pretty-format": "^23.0.0-alpha.2"
"pretty-format": "^23.0.0-alpha.4"
}
}
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