Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
586b17a
Do not diff attribute which match asymmetricMatcher.
WeiAnAn Dec 2, 2019
ec68d66
Use 'new' to copy RegExp. Fix calling method error after copied by Ob…
WeiAnAn Dec 2, 2019
4f7c31a
Use jest-util deepCopy to copy record.
WeiAnAn Dec 2, 2019
e69b816
update snapshot
WeiAnAn Dec 2, 2019
727d5e5
Merge branch 'master' into asymmetric_matcher_diff
WeiAnAn Dec 2, 2019
2c88e99
update changelog
WeiAnAn Dec 2, 2019
3885f56
remove line
WeiAnAn Dec 2, 2019
7a04cb0
sort changelog
WeiAnAn Dec 3, 2019
48ab232
sort path
WeiAnAn Dec 3, 2019
69fbc5d
move replaceMatchedToAsymmetricMatcher to jest-matcher-utils
WeiAnAn Dec 7, 2019
149783b
fix object attribute order
WeiAnAn Dec 7, 2019
883f4cd
add `ignoreAsymmetricMatches` option
WeiAnAn Dec 7, 2019
a6632ed
update docs
WeiAnAn Dec 7, 2019
4f5c1a9
update snapshot
WeiAnAn Dec 7, 2019
29154cd
add e2e test
WeiAnAn Dec 7, 2019
e99e985
merge master
WeiAnAn Dec 7, 2019
7aa6f27
update changelog
WeiAnAn Dec 7, 2019
b148c0f
ignore e2e test file
WeiAnAn Dec 7, 2019
fd6443e
lint
WeiAnAn Dec 7, 2019
ba5d7ec
update snapshot
WeiAnAn Dec 7, 2019
34c18dc
fix jest-circus doesn't get ignoreAsymmetricMatches value
WeiAnAn Dec 7, 2019
e0a6288
only import deepCyclicCopy to avoid loading incompatible packages in …
WeiAnAn Dec 7, 2019
c5361b4
lint
WeiAnAn Dec 7, 2019
c90a4ed
prettier Configuration.md
WeiAnAn Dec 7, 2019
d7c9a55
add copyright headers
WeiAnAn Dec 7, 2019
b438d6d
update snapshot
WeiAnAn Dec 7, 2019
65bd0cc
remove ignoreAsymmetricMatches option
WeiAnAn Jan 4, 2020
61f2c9f
support copy built-in objects
WeiAnAn Jan 5, 2020
4c0f959
add Class Replaceable to provide same api for object operation
WeiAnAn Jan 5, 2020
03e00c3
use Replaceable for replacing to asymmetrict matcher
WeiAnAn Jan 5, 2020
9a81ffc
lint
WeiAnAn Jan 5, 2020
67c443e
update changelog
WeiAnAn Jan 5, 2020
263f9a7
add braces
WeiAnAn Jan 5, 2020
d5863fa
change any type to unknow
WeiAnAn Jan 5, 2020
8f5321a
import correctly
WeiAnAn Jan 5, 2020
83ddb57
avoid browser read property of undefined
WeiAnAn Jan 5, 2020
4d4b7ce
update changelog
WeiAnAn Jan 5, 2020
d9dd2c4
Check value before checking constructor.
WeiAnAn Jan 14, 2020
d4c2f5e
restore deepCyclicCopy
WeiAnAn Jan 18, 2020
1db1b73
create deepCyclicCopyReplaceable
WeiAnAn Jan 18, 2020
739d835
change to use deepCyclicCopyReplaceable for copy
WeiAnAn Jan 18, 2020
3e200ce
undo no use change
WeiAnAn Jan 18, 2020
829ef89
drop dependency
WeiAnAn Jan 18, 2020
684b8ff
remove outdated changelog
WeiAnAn Jan 18, 2020
0a26b71
add braces
WeiAnAn Jan 19, 2020
0f441f4
undo outdated change
WeiAnAn Jan 19, 2020
c63cc72
Merge branch 'master' into asymmetric_matcher_diff
SimenB Jan 19, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
- `[jest-utils]` Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136))
- `[pretty-format]` Correctly detect memoized elements ([#9196](https://github.com/facebook/jest/pull/9196))
- `[jest-fake-timers]` Support `util.promisify` on `setTimeout` ([#9180](https://github.com/facebook/jest/pull/9180))
- `[jest-diff]` Do not highlight matched asymmetricMatcher in diffs ([#9257](https://github.com/facebook/jest/pull/9257))
- `[jest-util]` Use `new` to `deepCyclicCopy` RegExp. Fix error of calling method after copied by `Object.create` ([#9257](https://github.com/facebook/jest/pull/9257))

### Chore & Maintenance

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ exports[`.toBe() fails for: {"a": [Function a], "b": 2} and {"a": Any<Function>,

<d>If it should pass with deep equality, replace "toBe" with "toStrictEqual"</>

<d>Compared values serialize to the same structure.</>
<d>Printing internal object structure without calling \`toJSON\` instead.</>

<g>- Expected - 1</>
<r>+ Received + 1</>

Expand Down
1 change: 1 addition & 0 deletions packages/jest-diff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"chalk": "^3.0.0",
"diff-sequences": "^24.9.0",
"jest-get-type": "^24.9.0",
"jest-util": "^24.9.0",
"pretty-format": "^24.9.0"
},
"devDependencies": {
Expand Down
112 changes: 96 additions & 16 deletions packages/jest-diff/src/__tests__/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,24 +192,104 @@ line 4`;
});

describe('objects', () => {
const a = {a: {b: {c: 5}}};
const b = {a: {b: {c: 6}}};
const expected = [
' Object {',
' "a": Object {',
' "b": Object {',
'- "c": 5,',
'+ "c": 6,',
' },',
' },',
' }',
].join('\n');
describe('expand', () => {
const a = {a: {b: {c: 5}}};
const b = {a: {b: {c: 6}}};
const expected = [
' Object {',
' "a": Object {',
' "b": Object {',
'- "c": 5,',
'+ "c": 6,',
' },',
' },',
' }',
].join('\n');

test('(unexpanded)', () => {
expect(diff(a, b, unexpandedBe)).toBe(expected);
test('(unexpanded)', () => {
expect(diff(a, b, unexpandedBe)).toBe(expected);
});
test('(expanded)', () => {
expect(diff(a, b, expandedBe)).toBe(expected);
});
});
test('(expanded)', () => {
expect(diff(a, b, expandedBe)).toBe(expected);

describe('asymmetricMatcher', () => {
test('jest asymmetricMatcher', () => {
const a = {
a: expect.any(Number),
b: expect.anything(),
c: expect.arrayContaining([1, 3]),
d: 'jest is awesome',
e: 'jest is awesome',
f: {
a: new Date(),
b: 'jest is awesome',
},
g: true,
};
const b = {
a: 1,
b: 'anything',
c: [1, 2, 3],
d: expect.stringContaining('jest'),
e: expect.stringMatching(/^jest/),
f: expect.objectContaining({
a: expect.any(Date),
}),
g: false,
};
const expected = [
' Object {',
' "a": Any<Number>,',
' "b": Anything,',
' "c": ArrayContaining [',
' 1,',
' 3,',
' ],',
' "d": StringContaining "jest",',
' "e": StringMatching /^jest/,',
' "f": ObjectContaining {',
' "a": Any<Date>,',
' },',
'- "g": true,',
'+ "g": false,',
' }',
].join('\n');
expect(diff(a, b, optionsBe)).toBe(expected);
});

test('custom asymmetricMatcher', () => {
expect.extend({
equal5(received: any) {
if (received === 5)
return {
message: () => `expected ${received} not to be 5`,
pass: true,
};
return {
message: () => `expected ${received} to be 5`,
pass: false,
};
},
});
const a = {
a: 5,
b: true,
};
const b = {
a: expect.equal5(),
b: false,
};
const expected = [
' Object {',
' "a": equal5<>,',
'- "b": true,',
'+ "b": false,',
' }',
].join('\n');
expect(diff(a, b, optionsBe)).toBe(expected);
});
});
});

Expand Down
32 changes: 28 additions & 4 deletions packages/jest-diff/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import prettyFormat = require('pretty-format');
import chalk = require('chalk');
import getType = require('jest-get-type');
import {deepCyclicCopy} from 'jest-util';
import {DIFF_DELETE, DIFF_EQUAL, DIFF_INSERT, Diff} from './cleanupSemantic';
import {diffLinesRaw, diffLinesUnified, diffLinesUnified2} from './diffLines';
import {diffStringsRaw, diffStringsUnified} from './printDiffs';
Expand Down Expand Up @@ -131,16 +132,17 @@ function compareObjects(
) {
let difference;
let hasThrown = false;
const {replacedA, replacedB} = replaceMatchedToAsymmetricMatcher(a, b);

try {
const aCompare = prettyFormat(a, FORMAT_OPTIONS_0);
const bCompare = prettyFormat(b, FORMAT_OPTIONS_0);
const aCompare = prettyFormat(replacedA, FORMAT_OPTIONS_0);
const bCompare = prettyFormat(replacedB, FORMAT_OPTIONS_0);

if (aCompare === bCompare) {
difference = NO_DIFF_MESSAGE;
} else {
const aDisplay = prettyFormat(a, FORMAT_OPTIONS);
const bDisplay = prettyFormat(b, FORMAT_OPTIONS);
const aDisplay = prettyFormat(replacedA, FORMAT_OPTIONS);
const bDisplay = prettyFormat(replacedB, FORMAT_OPTIONS);

difference = diffLinesUnified2(
aDisplay.split('\n'),
Expand Down Expand Up @@ -183,4 +185,26 @@ function compareObjects(
return difference;
}

function replaceMatchedToAsymmetricMatcher(
a: Record<string, any>,
b: Record<string, any>,
) {
const replacedA = deepCyclicCopy(a, {keepPrototype: true});
const replacedB = deepCyclicCopy(b, {keepPrototype: true});
Object.entries(replacedB).forEach(([key, bValue]) => {
const aValue = a[key];
if (isAsymmetricMatcher(bValue)) {
if (bValue.asymmetricMatch(aValue)) replacedA[key] = bValue;
} else if (isAsymmetricMatcher(aValue)) {
if (aValue.asymmetricMatch(bValue)) replacedB[key] = aValue;
}
});
return {replacedA, replacedB};
}

function isAsymmetricMatcher(data: any) {
const type = getType(data);
return type === 'object' && typeof data.asymmetricMatch === 'function';
}

export default diff;
3 changes: 2 additions & 1 deletion packages/jest-diff/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
{"path": "../diff-sequences"},
{"path": "../jest-get-type"},
{"path": "../pretty-format"},
{"path": "../test-utils"}
{"path": "../test-utils"},
{"path": "../jest-util"}
]
}
5 changes: 5 additions & 0 deletions packages/jest-util/src/__tests__/deepCyclicCopy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,8 @@ it('keeps the prototype for objects when keepPrototype = true', () => {
);
spy.mockRestore();
});

it('RegExp should work correctly after copied', () => {
const regExp = deepCyclicCopy(/jest/i, {keepPrototype: true});
expect(regExp.test('JEST is awesome')).toBe(true);
});
6 changes: 6 additions & 0 deletions packages/jest-util/src/deepCyclicCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ function deepCyclicCopyObject<T>(
options: DeepCyclicCopyOptions,
cycles: WeakMap<any, any>,
): T {
if (object instanceof RegExp && options.keepPrototype) {
const result = new RegExp(object.source, object.flags);
result.lastIndex = object.lastIndex;
return result as typeof object;
}

const newObject = options.keepPrototype
? Object.create(Object.getPrototypeOf(object))
: {};
Expand Down