Skip to content

Commit

Permalink
feat: better error message for Map
Browse files Browse the repository at this point in the history
  • Loading branch information
pcorpet committed Jan 22, 2019
1 parent 1cededa commit c8c58a7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/chai/utils/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ function formatValue(ctx, value, recurseTimes) {
var base = ''
, array = false
, typedArray = false
, map = false
, braces = ['{', '}'];

if (isTypedArray(value)) {
Expand All @@ -144,6 +145,12 @@ function formatValue(ctx, value, recurseTimes) {
braces = ['[', ']'];
}

// Make Map say that they are Map
if (isMap(value)) {
map = true;
keys = Array.from(value.keys());
}

// Make functions say that they are functions
if (typeof value === 'function') {
name = getName(value);
Expand Down Expand Up @@ -185,6 +192,10 @@ function formatValue(ctx, value, recurseTimes) {
output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
} else if (typedArray) {
return formatTypedArray(value);
} else if (map) {
output = keys.map(function(key) {
return formatValue(ctx, key, recurseTimes) + ' => ' + formatValue(ctx, value.get(key), recurseTimes);
});
} else {
output = keys.map(function(key) {
return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
Expand Down Expand Up @@ -359,6 +370,10 @@ function isArray(ar) {
(typeof ar === 'object' && objectToString(ar) === '[object Array]');
}

function isMap(m) {
return typeof m === 'object' && objectToString(m) === '[object Map]';
}

function isRegExp(re) {
return typeof re === 'object' && objectToString(re) === '[object RegExp]';
}
Expand Down
7 changes: 7 additions & 0 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,13 @@ describe('assert', function () {
err(function () {
assert.deepEqual(obj1, obj2);
}, "expected { tea: \'chai\' } to deeply equal { tea: \'black\' }");

var map1 = new Map([['a', 'a']])
, map2 = new Map([['a', 'b']]);

err(function () {
assert.deepEqual(map1, map2);
}, "expected { 'a' => 'a' } to deeply equal { 'a' => 'b' }")
});

it('deepEqual (ordering)', function() {
Expand Down

0 comments on commit c8c58a7

Please sign in to comment.