Last active
November 17, 2015 08:39
-
-
Save 599316527/7ad8b7a24d9a47071a31 to your computer and use it in GitHub Desktop.
Customizing Jasmine Global Json Diff Matchers
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
/** | |
* @file customizing jasmine global json diff matchers | |
* @author Kyle He ([email protected]) | |
*/ | |
var path = require('path'); | |
var jsonDiffPatch = require('jsondiffpatch'); | |
var Jasmine = require('jasmine'); | |
var jasmine = new Jasmine({ | |
projectBaseDir: path.resolve() | |
}); | |
// `jasmine.addMatchers` is only allowed being called in beforeEach | |
// use `jasmine.Expectation.addCoreMatchers` method to add global matchers | |
jasmine.jasmine.Expectation.addCoreMatchers({ | |
toEqualJSON: function(util, customEqualityTesters) { | |
// Via. https://github.com/jasmine/jasmine/issues/675#issuecomment-127187623 | |
return { | |
compare: function(actual, expected) { | |
var result = {}; | |
result.pass = util.equals(actual, expected, customEqualityTesters); | |
result.name = 'JSON objects ' + (result.pass ? '' : 'don\'t') + ' match'; | |
if (result.pass) { | |
result.message = 'OMG Big Equal!'; | |
} else { | |
result.message = '' + jsonDiffPatch.formatters.console.format(jsonDiffPatch.diff(expected, actual)); | |
} | |
return result; | |
} | |
}; | |
} | |
}); | |
jasmine.loadConfigFile(path.resolve(__dirname, '../test/jasmine.json')); | |
jasmine.showColors(true); | |
jasmine.execute(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment