Skip to content

Commit

Permalink
Do not highlight matched asymmetricMatcher in diffs (#9257)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeiAnAn authored and SimenB committed Jan 19, 2020
1 parent 2839036 commit 2ece4f9
Show file tree
Hide file tree
Showing 9 changed files with 790 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- `[@jest/fake-timers]` Add Lolex as implementation of fake timers ([#8897](https://github.com/facebook/jest/pull/8897))
- `[jest-get-type]` Add `BigInt` support. ([#8382](https://github.com/facebook/jest/pull/8382))
- `[jest-matcher-utils]` Add `BigInt` support to `ensureNumbers` `ensureActualIsNumber`, `ensureExpectedIsNumber` ([#8382](https://github.com/facebook/jest/pull/8382))
- `[jest-matcher-utils]` Ignore highlighting matched asymmetricMatcher in diffs ([#9257](https://github.com/facebook/jest/pull/9257))
- `[jest-reporters]` Export utils for path formatting ([#9162](https://github.com/facebook/jest/pull/9162))
- `[jest-reporters]` Provides global coverage thresholds as watermarks for istanbul ([#9416](https://github.com/facebook/jest/pull/9416))
- `[jest-runner]` Warn if a worker had to be force exited ([#8206](https://github.com/facebook/jest/pull/8206))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,8 @@ 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"</>

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

<d> Object {</>
<g>- "a": Any<Function>,</>
<r>+ "a": [Function a],</>
<d> "b": 2,</>
<d> }</>
Expected: <g>{"a": Any<Function>, "b": 2}</>
Received: <r>{"a": [Function a], "b": 2}</>
`;

exports[`.toBe() fails for: {"a": 1} and {"a": 1} 1`] = `
Expand Down
56 changes: 56 additions & 0 deletions packages/jest-matcher-utils/src/Replaceable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

import getType = require('jest-get-type');

const supportTypes = ['map', 'array', 'object'];

type ReplaceableForEachCallBack = (value: any, key: any, object: any) => void;

export default class Replaceable {
object: any;
type: string;

constructor(object: any) {
this.object = object;
this.type = getType(object);
if (!supportTypes.includes(this.type)) {
throw new Error(`Type ${this.type} is not support in Replaceable!`);
}
}

static isReplaceable(obj1: any, obj2: any): boolean {
const obj1Type = getType(obj1);
const obj2Type = getType(obj2);
return obj1Type === obj2Type && supportTypes.includes(obj1Type);
}

forEach(cb: ReplaceableForEachCallBack): void {
if (this.type === 'object') {
Object.entries(this.object).forEach(([key, value]) => {
cb(value, key, this.object);
});
} else {
this.object.forEach(cb);
}
}

get(key: any): any {
if (this.type === 'map') {
return this.object.get(key);
}
return this.object[key];
}

set(key: any, value: any): void {
if (this.type === 'map') {
this.object.set(key, value);
} else {
this.object[key] = value;
}
}
}
154 changes: 154 additions & 0 deletions packages/jest-matcher-utils/src/__tests__/Replaceable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. 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.
*/

import Replaceable from '../Replaceable';

describe('Replaceable', () => {
describe('constructor', () => {
test('init with object', () => {
const replaceable = new Replaceable({a: 1, b: 2});
expect(replaceable.object).toEqual({a: 1, b: 2});
expect(replaceable.type).toBe('object');
});

test('init with array', () => {
const replaceable = new Replaceable([1, 2, 3]);
expect(replaceable.object).toEqual([1, 2, 3]);
expect(replaceable.type).toBe('array');
});

test('init with Map', () => {
const replaceable = new Replaceable(
new Map([
['a', 1],
['b', 2],
]),
);
expect(replaceable.object).toEqual(
new Map([
['a', 1],
['b', 2],
]),
);
expect(replaceable.type).toBe('map');
});

test('init with other type should throw error', () => {
expect(() => {
//eslint-disable-next-line @typescript-eslint/no-unused-vars
const replaceable = new Replaceable(new Date());
}).toThrow('Type date is not support in Replaceable!');
});
});

describe('get', () => {
test('get object item', () => {
const replaceable = new Replaceable({a: 1, b: 2});
expect(replaceable.get('b')).toBe(2);
});

test('get array item', () => {
const replaceable = new Replaceable([1, 2, 3]);
expect(replaceable.get(1)).toBe(2);
});

test('get Map item', () => {
const replaceable = new Replaceable(
new Map([
['a', 1],
['b', 2],
]),
);
expect(replaceable.get('b')).toBe(2);
});
});

describe('set', () => {
test('set object item', () => {
const replaceable = new Replaceable({a: 1, b: 2});
replaceable.set('b', 3);
expect(replaceable.object).toEqual({a: 1, b: 3});
});

test('set array item', () => {
const replaceable = new Replaceable([1, 2, 3]);
replaceable.set(1, 3);
expect(replaceable.object).toEqual([1, 3, 3]);
});

test('set Map item', () => {
const replaceable = new Replaceable(
new Map([
['a', 1],
['b', 2],
]),
);
replaceable.set('b', 3);
expect(replaceable.object).toEqual(
new Map([
['a', 1],
['b', 3],
]),
);
});
});

describe('forEach', () => {
test('object forEach', () => {
const replaceable = new Replaceable({a: 1, b: 2});
const cb = jest.fn();
replaceable.forEach(cb);
expect(cb.mock.calls[0]).toEqual([1, 'a', {a: 1, b: 2}]);
expect(cb.mock.calls[1]).toEqual([2, 'b', {a: 1, b: 2}]);
});

test('array forEach', () => {
const replaceable = new Replaceable([1, 2, 3]);
const cb = jest.fn();
replaceable.forEach(cb);
expect(cb.mock.calls[0]).toEqual([1, 0, [1, 2, 3]]);
expect(cb.mock.calls[1]).toEqual([2, 1, [1, 2, 3]]);
expect(cb.mock.calls[2]).toEqual([3, 2, [1, 2, 3]]);
});

test('map forEach', () => {
const map = new Map([
['a', 1],
['b', 2],
]);
const replaceable = new Replaceable(map);
const cb = jest.fn();
replaceable.forEach(cb);
expect(cb.mock.calls[0]).toEqual([1, 'a', map]);
expect(cb.mock.calls[1]).toEqual([2, 'b', map]);
});
});

describe('isReplaceable', () => {
test('should return true if two object types equal and support', () => {
expect(Replaceable.isReplaceable({a: 1}, {b: 2})).toBe(true);
expect(Replaceable.isReplaceable([], [1, 2, 3])).toBe(true);
expect(
Replaceable.isReplaceable(
new Map(),
new Map([
['a', 1],
['b', 2],
]),
),
).toBe(true);
});

test('should return false if two object types not equal', () => {
expect(Replaceable.isReplaceable({a: 1}, [1, 2, 3])).toBe(false);
});

test('should return false if object types not support', () => {
expect(Replaceable.isReplaceable('foo', 'bar')).toBe(false);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,160 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`printDiffOrStringify asymmetricMatcher array 1`] = `
<g>- Expected - 1</>
<r>+ Received + 1</>
<d> Array [</>
<d> 1,</>
<d> Any<Number>,</>
<g>- 3,</>
<r>+ 2,</>
<d> ]</>
`;
exports[`printDiffOrStringify asymmetricMatcher circular array 1`] = `
<g>- Expected - 1</>
<r>+ Received + 1</>
<d> Array [</>
<d> 1,</>
<d> Any<Number>,</>
<g>- 3,</>
<r>+ 2,</>
<d> [Circular],</>
<d> ]</>
`;
exports[`printDiffOrStringify asymmetricMatcher circular map 1`] = `
<g>- Expected - 2</>
<r>+ Received + 2</>
<d> Map {</>
<d> "a" => 1,</>
<d> "b" => Any<Number>,</>
<g>- "c" => 3,</>
<r>+ "c" => 2,</>
<d> "circular" => Map {</>
<d> "a" => 1,</>
<d> "b" => Any<Number>,</>
<g>- "c" => 3,</>
<r>+ "c" => 2,</>
<d> "circular" => [Circular],</>
<d> },</>
<d> }</>
`;
exports[`printDiffOrStringify asymmetricMatcher circular object 1`] = `
<g>- Expected - 1</>
<r>+ Received + 1</>
<d> Object {</>
<d> "a": [Circular],</>
<d> "b": Any<Number>,</>
<g>- "c": 3,</>
<r>+ "c": 2,</>
<d> }</>
`;
exports[`printDiffOrStringify asymmetricMatcher custom asymmetricMatcher 1`] = `
<g>- Expected - 1</>
<r>+ Received + 1</>
<d> Object {</>
<d> "a": equal5<>,</>
<g>- "b": false,</>
<r>+ "b": true,</>
<d> }</>
`;
exports[`printDiffOrStringify asymmetricMatcher jest asymmetricMatcher 1`] = `
<g>- Expected - 1</>
<r>+ Received + 1</>
<d> Object {</>
<d> "a": Any<Number>,</>
<d> "b": Anything,</>
<d> "c": ArrayContaining [</>
<d> 1,</>
<d> 3,</>
<d> ],</>
<d> "d": StringContaining "jest",</>
<d> "e": StringMatching /^jest/,</>
<d> "f": ObjectContaining {</>
<d> "a": Any<Date>,</>
<d> },</>
<g>- "g": true,</>
<r>+ "g": false,</>
<d> }</>
`;
exports[`printDiffOrStringify asymmetricMatcher map 1`] = `
<g>- Expected - 1</>
<r>+ Received + 1</>
<d> Map {</>
<d> "a" => 1,</>
<d> "b" => Any<Number>,</>
<g>- "c" => 3,</>
<r>+ "c" => 2,</>
<d> }</>
`;
exports[`printDiffOrStringify asymmetricMatcher minimal test 1`] = `
<g>- Expected - 1</>
<r>+ Received + 1</>
<d> Object {</>
<d> "a": Any<Number>,</>
<g>- "b": 2,</>
<r>+ "b": 1,</>
<d> }</>
`;
exports[`printDiffOrStringify asymmetricMatcher nested object 1`] = `
<g>- Expected - 1</>
<r>+ Received + 1</>
<d> Object {</>
<d> "a": Any<Number>,</>
<d> "b": Object {</>
<d> "a": 1,</>
<d> "b": Any<Number>,</>
<d> },</>
<g>- "c": 2,</>
<r>+ "c": 1,</>
<d> }</>
`;
exports[`printDiffOrStringify asymmetricMatcher object in array 1`] = `
<g>- Expected - 1</>
<r>+ Received + 1</>
<d> Array [</>
<d> 1,</>
<d> Object {</>
<d> "a": 1,</>
<d> "b": Any<Number>,</>
<d> },</>
<g>- 3,</>
<r>+ 2,</>
<d> ]</>
`;
exports[`printDiffOrStringify asymmetricMatcher transitive circular 1`] = `
<g>- Expected - 1</>
<r>+ Received + 1</>
<d> Object {</>
<g>- "a": 3,</>
<r>+ "a": 2,</>
<d> "nested": Object {</>
<d> "b": Any<Number>,</>
<d> "parent": [Circular],</>
<d> },</>
<d> }</>
`;
exports[`printDiffOrStringify expected and received are multi line with trailing spaces 1`] = `
<g>- Expected - 3</>
<r>+ Received + 3</>
Expand Down
Loading

0 comments on commit 2ece4f9

Please sign in to comment.