Created
December 5, 2014 13:45
-
-
Save trodrigues/4aead044ab1a81f1cf02 to your computer and use it in GitHub Desktop.
Deep object diff matcher for Jasmine 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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