Skip to content

Commit

Permalink
test: fix Matcher.toEqual to not depend on key insertion order (#2091)
Browse files Browse the repository at this point in the history
Objects `{foo: 1, bar: 2}` and `{bar: 2, foo: 1}` should be considered
equal.
  • Loading branch information
aslushnikov authored Mar 15, 2018
1 parent 4b7fbf8 commit afcc74e
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions utils/testrunner/Matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ const DefaultMatchers = {
},

toEqual: function(value, other, message) {
message = message || `${JSON.stringify(value)}${JSON.stringify(other)}`;
return { pass: JSON.stringify(value) === JSON.stringify(other), message };
const valueJson = stringify(value);
const otherJson = stringify(other);
message = message || `${valueJson}${otherJson}`;
return { pass: valueJson === otherJson, message };
},

toBeCloseTo: function(value, other, precision, message) {
Expand All @@ -107,3 +109,16 @@ const DefaultMatchers = {
};
}
};

function stringify(value) {
function stabilize(key, object) {
if (typeof object !== 'object' || object === undefined || object === null)
return object;
const result = {};
for (const key of Object.keys(object).sort())
result[key] = object[key];
return result;
}

return JSON.stringify(stabilize(null, value), stabilize);
}

0 comments on commit afcc74e

Please sign in to comment.