Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pretty-inmutable for better error message [issue-2489] #2529

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

const chalk = require('chalk');
const prettyFormat = require('pretty-format');
const ImmutablePlugin = require('pretty-format/build/plugins/Inmutable');

export type ValueType =
| 'array'
Expand Down Expand Up @@ -89,6 +90,7 @@ const stringify = (object: any, maxDepth?: number = 10): string => {
result = prettyFormat(object, {
maxDepth,
min: true,
plugins: [ImmutablePlugin],
});
} catch (e) {
result = prettyFormat(object, {
Expand Down
4 changes: 3 additions & 1 deletion packages/pretty-format/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"perf": "node perf/test.js"
},
"dependencies": {
"ansi-styles": "^2.2.1"
"ansi-styles": "^2.2.1",
"immutable": "^3.8.1",
"pretty-immutable": "^1.0.1"
}
}
23 changes: 23 additions & 0 deletions packages/pretty-format/src/plugins/Inmutable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/* eslint-disable max-len */
'use strict';
const prettyI = require('pretty-immutable');
const InmutableTypes = ['List', 'OrderedSet'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo here and this list is not exhaustive either.

const isInMutable = val => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem like an efficient way to determine what type it is.

return (element, index, array) => val.toString().includes(element);
};

module.exports = {
print(val, print, indent) {
return 'Inmutable ' + prettyI(val);
},
test(val) {
return val && InmutableTypes.some(isInMutable(val));
},
};
26 changes: 26 additions & 0 deletions packages/pretty-format/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


ansi-styles@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe"

immutable@^3.8.1:
version "3.8.1"
resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.8.1.tgz#200807f11ab0f72710ea485542de088075f68cd2"

json-stringify-pretty-compact@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/json-stringify-pretty-compact/-/json-stringify-pretty-compact-1.0.2.tgz#abe8ae193feb19020dfeec74363be03d65821a31"

pretty-immutable@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/pretty-immutable/-/pretty-immutable-1.0.1.tgz#0a41d508fea2a6099c330f5d998e65722b3580a0"
dependencies:
json-stringify-pretty-compact "^1.0.1"
repeat-string "^1.5.4"

repeat-string@^1.5.4:
version "1.6.1"
resolved "https://registry.yarnpkg.com/repeat-string/-/repeat-string-1.6.1.tgz#8dcae470e1c88abc2d600fff4a776286da75e637"