karma-jasmine-diff-reporter
Advanced tools
Comparing version 0.6.0 to 0.6.1
### Changelog | ||
- 0.6.1 - Sort object properties before comparing | ||
- 0.6.0 - Add a color option to highlight whitespaces independently | ||
@@ -4,0 +5,0 @@ - 0.5.0 - Added JSON option. Fixed pretty print for Karma 1.x |
@@ -20,4 +20,28 @@ describe('Matcher', function () { | ||
it('should sort object props', function () { | ||
var a = { | ||
bar: 42, | ||
qux: 'fox', | ||
foo: { | ||
c: 2, | ||
a: 0, | ||
b: 1, | ||
} | ||
}; | ||
var b = { | ||
far: 50, | ||
qux: 'fox', | ||
extra: 'extra', | ||
bar: 42, | ||
foo: { | ||
b: 1, | ||
c: 2, | ||
a: 0, | ||
} | ||
}; | ||
expect(a).toEqual(b); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "karma-jasmine-diff-reporter", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "Karma reporter to highlight diffs of failed equality expectations for Jasmine", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -23,2 +23,19 @@ (function (window) { | ||
// https://github.com/mradionov/karma-jasmine-diff-reporter/issues/16 | ||
// Objects might have their properties specified in different order, it might | ||
// result in a not very correct diff, when the same prop appears in different | ||
// places of compared objects. Fix it by soring object properties in | ||
// alphabetical order. | ||
// NOTE: Order of props is not guaranteed in JS, | ||
// see http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order | ||
// But according to: http://stackoverflow.com/a/29622653/1573638 | ||
// "most of the browser's implementations values in objects are stored | ||
// in the order in which they were added" - we can use it, it won't harm anyway | ||
function sortObject(obj) { | ||
return Object.keys(obj).sort().reduce(function (result, key) { | ||
result[key] = obj[key]; | ||
return result; | ||
}, {}); | ||
} | ||
var toString = Object.prototype.toString; | ||
@@ -30,3 +47,7 @@ | ||
function isPlainObject(value) { | ||
return toString.call(value) === '[object Object]'; | ||
} | ||
function ppJSONPatched(j$) { | ||
@@ -47,2 +68,6 @@ return function(value) { | ||
if (isPlainObject(value)) { | ||
return sortObject(value); | ||
} | ||
return value; | ||
@@ -49,0 +74,0 @@ }); |
@@ -22,4 +22,5 @@ // Most of the code is originaly written by Jasmine developers. | ||
// Version of pretty print module taken from Jamsine 2.3.4 | ||
// At the moment, the only thing patched is a method | ||
// Patched methods: | ||
// StringPrettyPrinter.prototype.emitString | ||
// StringPrettyPrinter.prototype.emitObject | ||
@@ -42,2 +43,18 @@ window.jasmine.pp = ppPatched(window.jasmine); | ||
// https://github.com/mradionov/karma-jasmine-diff-reporter/issues/16 | ||
// Objects might have their properties specified in different order, it might | ||
// result in a not very correct diff, when the same prop appears in different | ||
// places of compared objects. Fix it by soring object properties in | ||
// alphabetical order. | ||
// NOTE: Order of props is not guaranteed in JS, | ||
// see http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order | ||
// But according to: http://stackoverflow.com/a/29622653/1573638 | ||
// "most of the browser's implementations values in objects are stored | ||
// in the order in which they were added" - we can use it, it won't harm anyway | ||
function sortObject(obj) { | ||
return Object.keys(obj).sort().reduce(function (result, key) { | ||
result[key] = obj[key]; | ||
return result; | ||
}, {}); | ||
} | ||
@@ -172,3 +189,5 @@ function ppPatched(j$) { | ||
this.iterateObject(obj, function(property, isGetter) { | ||
var sortedObj = sortObject(obj); | ||
this.iterateObject(sortedObj, function(property, isGetter) { | ||
if (first) { | ||
@@ -180,3 +199,3 @@ first = false; | ||
self.formatProperty(obj, property, isGetter); | ||
self.formatProperty(sortedObj, property, isGetter); | ||
}); | ||
@@ -183,0 +202,0 @@ |
71640
1757