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.

Inspect doesn't work properly for Maps and Sets - It displays an empty object

See original GitHub issue

As I was implementing the .deep flag support on the keys assertion I noticed that utils.inspect isn’t working properly when it comes to Maps and Sets. Instead of returning the values inside the Map or Set it returns the following string: {}.

You can reproduce this behavior by doing, for example:

chai.use(function (_chai, utils) {
  var inspectSet = _utilsinspect(new Set([{iAmAn: 'item'}, {thisIs: 'anotherItem'}]));
  var inspectMap = utils.inspect(new Map([[{iAmA: 'key'}, 'aValue']]))
  console.log(inspectSet); // --> {}
  console.log(inspectMap); // --> {}
});

What do you guys think should be the desired output when inspecting maps or sets?

IMO the ideal output would be:

chai.use(function (_chai, utils) {
  var inspectSet = _utilsinspect(new Set([{iAmAn: 'item' }, {thisIs: 'anotherItem'}]));
  var inspectMap = utils.inspect(new Map([[{iAmA: 'key' }, 'aValue']]))
  console.log(inspectSet); // Desired --> [ {iAmAn: 'item' }, { thisIs: 'anotherItem' } ]
   console.log(inspectMap); // Desired --> [ [ { iAmA: 'key' }, 'aValue' ] ]
});

I think this wouldn’t be a difficult fix. Maybe adding a check alongside these other checks and then calling a function to iterate through every entry and format the Map/Set members should be enough. This will be very similiar to what we already do in formatArray.

I will make sure to submit a PR for this before implementing the deep flag for Maps/Sets because this will be very useful to create good test cases.

Please let me know if you’ve got any idea or opinion about this 😉

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
jfirebaughcommented, Jun 17, 2019

This got accidentally closed because #668’s description contained the text we need to fix #662 in order to enable all tests. Reopening.

2reactions
keithamuscommented, Apr 4, 2016

I’ve got some other bits I want to work on for chaijs/loupe so hold off on this one just for now. We’ll get this fixed for 4.0 though 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

map not is not a function on empty object? - Stack Overflow
I have set up a reducer with the state as an empty object. But when using the .map() function it doesn't seem to...
Read more >
Understanding Map and Set Objects in JavaScript | DigitalOcean
In this article, you will go over the Map and Set objects, ... Objects are not the same two Objects, despite having the...
Read more >
inspect — Inspect live objects — Python 3.11.1 documentation
Return a list of source lines and starting line number for an object. The argument may be a module, class, method, function, traceback,...
Read more >
Error Messages | Maps JavaScript API - Google Developers
The script element that loads the Maps JavaScript API is not being included correctly on your page. In order for the API to...
Read more >
How to Check if Object is Empty in JavaScript - Samantha Ming
Here's a Code Recipe to check if an object is empty or not. For newer browsers, you can use plain vanilla JS and...
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