Skip to content

Commit

Permalink
Pretty-print asymmetric matchers (jestjs#2476)
Browse files Browse the repository at this point in the history
* First attempt to fix this

* Extract asymmetric-matchers from jasmine-utils

* Remove <jasmine.*> adnotations

* Remove obsolete snapshots

* Add AsymmetricMatcher plugin for pretty-format

* Refactor asymmetric-matchers

* Use AsymmetricMatcher plugin in diffs

* Remove unused prettyFormat from asymmetric-matcher

* Check for object presence in AsymmetricMatcher

* Update snapshots

* Tests for asymmetric-matchers

* Update snapshots with patch marks

* Adjust printing of asymmetric matchers

* Use class instances instead of regex to render values
  • Loading branch information
thymikee authored and skovhus committed Apr 29, 2017
1 parent 9c099fc commit 90fa1e0
Show file tree
Hide file tree
Showing 10 changed files with 595 additions and 196 deletions.
7 changes: 6 additions & 1 deletion packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {DiffOptions} from './diffStrings';

const ReactElementPlugin = require('pretty-format/build/plugins/ReactElement');
const ReactTestComponentPlugin = require('pretty-format/build/plugins/ReactTestComponent');
const AsymmetricMatcherPlugin = require('pretty-format/build/plugins/AsymmetricMatcher');

const chalk = require('chalk');
const diffStrings = require('./diffStrings');
Expand All @@ -25,7 +26,11 @@ const {
SIMILAR_MESSAGE,
} = require('./constants');

const PLUGINS = [ReactTestComponentPlugin, ReactElementPlugin];
const PLUGINS = [
ReactTestComponentPlugin,
ReactElementPlugin,
AsymmetricMatcherPlugin,
];
const FORMAT_OPTIONS = {
plugins: PLUGINS,
};
Expand Down
7 changes: 7 additions & 0 deletions packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

const chalk = require('chalk');
const prettyFormat = require('pretty-format');
const AsymmetricMatcherPlugin = require('pretty-format/build/plugins/AsymmetricMatcher');

const PLUGINS = [AsymmetricMatcherPlugin];

export type ValueType =
| 'array'
Expand Down Expand Up @@ -71,6 +74,8 @@ const getType = (value: any): ValueType => {
return 'map';
} else if (value.constructor === Set) {
return 'set';
} else if (value.toString() === 'ArrayContaining') {
return 'array';
}
return 'object';
// $FlowFixMe https://github.com/facebook/flow/issues/1015
Expand All @@ -89,12 +94,14 @@ const stringify = (object: any, maxDepth?: number = 10): string => {
result = prettyFormat(object, {
maxDepth,
min: true,
plugins: PLUGINS,
});
} catch (e) {
result = prettyFormat(object, {
callToJSON: false,
maxDepth,
min: true,
plugins: PLUGINS,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.000004, 5]

Expected value not to be close to (with 5-digit precision):
0.000004
Received:
Received:
0"
`;

Expand All @@ -192,7 +192,7 @@ exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.0001, 3] 1

Expected value not to be close to (with 3-digit precision):
0.0001
Received:
Received:
0"
`;

Expand All @@ -201,7 +201,7 @@ exports[`.toBeCloseTo() accepts an optional precision argument: [0, 0.1, 0] 1`]

Expected value not to be close to (with 0-digit precision):
0.1
Received:
Received:
0"
`;

Expand All @@ -210,7 +210,7 @@ exports[`.toBeCloseTo() passes: [0, 0.001] 1`] = `

Expected value not to be close to (with 2-digit precision):
0.001
Received:
Received:
0"
`;

Expand All @@ -219,7 +219,7 @@ exports[`.toBeCloseTo() passes: [0, 0] 1`] = `

Expected value not to be close to (with 2-digit precision):
0
Received:
Received:
0"
`;

Expand All @@ -228,7 +228,7 @@ exports[`.toBeCloseTo() passes: [1.23, 1.225] 1`] = `

Expected value not to be close to (with 2-digit precision):
1.225
Received:
Received:
1.23"
`;

Expand All @@ -237,7 +237,7 @@ exports[`.toBeCloseTo() passes: [1.23, 1.226] 1`] = `

Expected value not to be close to (with 2-digit precision):
1.226
Received:
Received:
1.23"
`;

Expand All @@ -246,7 +246,7 @@ exports[`.toBeCloseTo() passes: [1.23, 1.229] 1`] = `

Expected value not to be close to (with 2-digit precision):
1.229
Received:
Received:
1.23"
`;

Expand All @@ -255,7 +255,7 @@ exports[`.toBeCloseTo() passes: [1.23, 1.234] 1`] = `

Expected value not to be close to (with 2-digit precision):
1.234
Received:
Received:
1.23"
`;

Expand All @@ -264,7 +264,7 @@ exports[`.toBeCloseTo() throws: [0, 0.01] 1`] = `

Expected value to be close to (with 2-digit precision):
0.01
Received:
Received:
0"
`;

Expand All @@ -273,7 +273,7 @@ exports[`.toBeCloseTo() throws: [1, 1.23] 1`] = `

Expected value to be close to (with 2-digit precision):
1.23
Received:
Received:
1"
`;

Expand All @@ -282,7 +282,7 @@ exports[`.toBeCloseTo() throws: [1.23, 1.2249999] 1`] = `

Expected value to be close to (with 2-digit precision):
1.2249999
Received:
Received:
1.23"
`;

Expand Down Expand Up @@ -1742,20 +1742,20 @@ Received:
\"abc\""
`;

exports[`.toEqual() expect("abcd").not.toEqual({"regexp": /bc/}) 1`] = `
exports[`.toEqual() expect("abcd").not.toEqual(StringMatching /bc/) 1`] = `
"expect(received).not.toEqual(expected)

Expected value to not equal:
{\"regexp\": /bc/}
StringMatching /bc/
Received:
\"abcd\""
`;

exports[`.toEqual() expect("abd").toEqual({"regexp": /bc/i}) 1`] = `
exports[`.toEqual() expect("abd").toEqual(StringMatching /bc/i) 1`] = `
"expect(received).toEqual(expected)

Expected value to equal:
{\"regexp\": /bc/i}
StringMatching /bc/i
Received:
\"abd\"

Expand All @@ -1773,60 +1773,68 @@ Received:
\"banana\""
`;

exports[`.toEqual() expect([1, 2, 3]).not.toEqual({"sample": [2, 3]}) 1`] = `
exports[`.toEqual() expect([1, 2, 3]).not.toEqual(ArrayContaining [2, 3]) 1`] = `
"expect(received).not.toEqual(expected)

Expected value to not equal:
{\"sample\": [2, 3]}
ArrayContaining [2, 3]
Received:
[1, 2, 3]"
`;

exports[`.toEqual() expect([1, 3]).toEqual({"sample": [1, 2]}) 1`] = `
exports[`.toEqual() expect([1, 3]).toEqual(ArrayContaining [1, 2]) 1`] = `
"expect(received).toEqual(expected)

Expected value to equal:
{\"sample\": [1, 2]}
ArrayContaining [1, 2]
Received:
[1, 3]

Difference:

Comparing two different types of values. Expected object but received array."
- Expected
+ Received

-ArrayContaining [
+Array [
 1,
- 2,
+ 3,
 ]"
`;

exports[`.toEqual() expect([Function anonymous]).not.toEqual({"expectedObject": [Function Function]}) 1`] = `
exports[`.toEqual() expect([Function anonymous]).not.toEqual(Any<Function>) 1`] = `
"expect(received).not.toEqual(expected)

Expected value to not equal:
{\"expectedObject\": [Function Function]}
Any<Function>
Received:
[Function anonymous]"
`;

exports[`.toEqual() expect({"a": 1, "b": [Function b], "c": true}).not.toEqual({"a": 1, "b": {"expectedObject": [Function Function]}, "c": {}}) 1`] = `
exports[`.toEqual() expect({"a": 1, "b": [Function b], "c": true}).not.toEqual({"a": 1, "b": Any<Function>, "c": Anything}) 1`] = `
"expect(received).not.toEqual(expected)

Expected value to not equal:
{\"a\": 1, \"b\": {\"expectedObject\": [Function Function]}, \"c\": {}}
{\"a\": 1, \"b\": Any<Function>, \"c\": Anything}
Received:
{\"a\": 1, \"b\": [Function b], \"c\": true}"
`;

exports[`.toEqual() expect({"a": 1, "b": 2}).not.toEqual({"sample": {"a": 1}}) 1`] = `
exports[`.toEqual() expect({"a": 1, "b": 2}).not.toEqual(ObjectContaining {"a": 1}) 1`] = `
"expect(received).not.toEqual(expected)

Expected value to not equal:
{\"sample\": {\"a\": 1}}
ObjectContaining {\"a\": 1}
Received:
{\"a\": 1, \"b\": 2}"
`;

exports[`.toEqual() expect({"a": 1, "b": 2}).toEqual({"sample": {"a": 2}}) 1`] = `
exports[`.toEqual() expect({"a": 1, "b": 2}).toEqual(ObjectContaining {"a": 2}) 1`] = `
"expect(received).toEqual(expected)

Expected value to equal:
{\"sample\": {\"a\": 2}}
ObjectContaining {\"a\": 2}
Received:
{\"a\": 1, \"b\": 2}

Expand All @@ -1836,9 +1844,7 @@ Difference:
+ Received

-ObjectContaining {
- \"sample\": Object {
- \"a\": 2,
- },
- \"a\": 2,
+Object {
+ \"a\": 1,
+ \"b\": 2,
Expand Down Expand Up @@ -1904,11 +1910,11 @@ Difference:
Comparing two different types of values. Expected undefined but received null."
`;

exports[`.toEqual() expect(true).not.toEqual({}) 1`] = `
exports[`.toEqual() expect(true).not.toEqual(Anything) 1`] = `
"expect(received).not.toEqual(expected)

Expected value to not equal:
{}
Anything
Received:
true"
`;
Expand All @@ -1931,11 +1937,11 @@ Received:
true"
`;

exports[`.toEqual() expect(undefined).toEqual({"expectedObject": [Function Function]}) 1`] = `
exports[`.toEqual() expect(undefined).toEqual(Any<Function>) 1`] = `
"expect(received).toEqual(expected)

Expected value to equal:
{\"expectedObject\": [Function Function]}
Any<Function>
Received:
undefined

Expand All @@ -1944,11 +1950,11 @@ Difference:
Comparing two different types of values. Expected object but received undefined."
`;

exports[`.toEqual() expect(undefined).toEqual({}) 1`] = `
exports[`.toEqual() expect(undefined).toEqual(Anything) 1`] = `
"expect(received).toEqual(expected)

Expected value to equal:
{}
Anything
Received:
undefined

Expand Down
Loading

0 comments on commit 90fa1e0

Please sign in to comment.