Skip to content

Instantly share code, notes, and snippets.

@trodrigues
Created December 5, 2014 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trodrigues/4aead044ab1a81f1cf02 to your computer and use it in GitHub Desktop.
Save trodrigues/4aead044ab1a81f1cf02 to your computer and use it in GitHub Desktop.
Deep object diff matcher for Jasmine 2
// use with https://www.npmjs.org/package/deep-diff
toEqualObj: function () {
var KINDS = {
'N': 'newly added property/element',
'D': 'property/element was deleted',
'E': 'property/element was edited',
'A': 'change occurred within an array'
};
function formatDiff(objdiff) {
return objdiff.map(function (diff) {
return '\t'+diff.kind +' at '+ diff.path.join('.') +'\n'+
'\t'+diff.actual +' should be '+ diff.expected;
}).join('\n\n');
}
return {
compare: function (actual, expected) {
var objdiff = (deepDiff(expected, actual) || []).map(function (diff) {
return {
kind: KINDS[diff.kind],
path: diff.path,
expected: diff.lhs,
actual: diff.rhs
};
});
return {
pass : objdiff.length === 0,
message : 'Expected object not to have differences\n\n' + formatDiff(objdiff)
};
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment