question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Opt-out of truncation for custom classes?

See original GitHub issue

The https://github.com/astorije/chai-immutable plugin extends lots of core assertions to work with ImmutableJS.

The collections in Immutable all have a custom toString() and inspect() method which produces a readable version of their internal datastructures. This gets picked up correctly by chai’s inspect util.

However, if this representation goes over the truncateThreshold, they are detected as objects and their first two properties are displayed instead - which isn’t useful for these collections.

To work around this, many of the assertion methods convert the objects to a string before passing to the message formatting. This mostly works but it’s easy to miss cases.

This problem should apply to any object with a custom inspect, would it be possible to tweak this behaviour somehow?

Some options I can think of would be to change the object detection to check for plain objects only, or to skip truncation for objects with a custom inspect() method, or to truncate the result of inspect() when objects have an inspect().

Any thoughts?

Two options I can think of

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
glenjamincommented, Jun 11, 2016

In the best case scenario, it uses Chai’s custom inspect utility, which is a close adaptation of Node’s util.inspect, although work is being done here to improve upon that. This plays nicely with Immutable collections thanks to their custom .toString property. It’s important to note that at no point in this process does Chai take into consideration an object’s .inspect property.

Immutable objects have .inspect() as an alias for .toString(), as far as I can tell this is what chai and/or loupe would pick: https://github.com/chaijs/chai/blob/master/lib/chai/utils/inspect.js#L45 https://github.com/chaijs/loupe/blob/master/lib/loupe.js#L61

On a related note, you may find https://github.com/Xotic750/inspect-x interesting, it’s also originally based on Node’s inspect and does a good job of handling the newer ES6 datatypes.

Personally, I’m not too crazy about how it’s handling arrays and objects. I think I’d prefer that it instead take the original .toString representation and cut it down to truncateThreshold length by chopping off from the end. And then adding a “…” at the end of it. I believe this would be more helpful in general, not just in your examples with the Immutable library, and would still respect the user’s truncateThreshold setting.

👍 from me on this, assuming you mean the result of the inspect util (and loupe in future). that would solve my immediate problem and should work well in general. Would you still want to handle values that were originally strings differently?

I’m also not in favor of Chai having knowledge of an object’s .inspect property. Unlike .toString, there’s nothing standard about .inspect.

I think it’s reasonably fair to say it’s a de-facto standard in Node-land, thanks to https://nodejs.org/dist/latest-v4.x/docs/api/util.html#util_custom_inspect_function_on_objects having been in the docs since at least 0.4 that I know of. Whether that’s a good enough reason to lean more heavily on it, I don’t know.

1reaction
glenjamincommented, Jun 22, 2016

That seems sensible, please let me know if there’s anything on there that I could help contribute with.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Truncate Custom Objects in Salesforce. - Biswajeet Samal
Truncating a custom object allows Salesforce to delete all records from particular objects and save the empty object and its metadata.
Read more >
Truncate Custom Objects - Salesforce Help
Go to the object management settings for custom objects. Click an object name to go to the object's detail page, and then click...
Read more >
Truncation and Performance | Functions That Use LOB Types ...
Truncation and Performance | Functions That Use LOB Types | Teradata Vantage - 17.00 - 17.05 ... Cogroup and Multiple Input Table Operators...
Read more >
Wrapping and Truncating Text | SAP Fiori Design Guidelines
There are three types of responsive behavior for text that doesn't fit onto a line: Wrapping; Truncation; A combination of wrapping and truncation...
Read more >
Liquid Filters - Braze
In a custom attribute array, this is the oldest added value. ✓ Yes ... is appended to the truncated string and is included...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found