mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
test: fix Matcher.toEqual to not depend on key insertion order (#2091)
Objects `{foo: 1, bar: 2}` and `{bar: 2, foo: 1}` should be considered equal.
This commit is contained in:
parent
4b7fbf8ee0
commit
afcc74e7c7
@ -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) {
|
||||
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user