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

feat: better error message for Map #1229

Closed
wants to merge 1 commit into from
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
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