Skip to content

Instantly share code, notes, and snippets.

@ichabodcole
Created December 17, 2014 06:58
Show Gist options
  • Save ichabodcole/c7c6a2007d18c94377ba to your computer and use it in GitHub Desktop.
Save ichabodcole/c7c6a2007d18c94377ba to your computer and use it in GitHub Desktop.
jasmine 2.1 - CustomMatcher -- toBeNearTo
jasmine.addMatchers({
toBeNearTo: function (utils, customEqualityTesters) {
return {
compare: function(actual, expected, within) {
var diff,
result = {};
within = Math.abs(within) || 1;
diff = Math.abs(actual - expected);
result.pass = diff <= within;
if (result.pass) {
result.message = "Expected " + actual + " to be within " + within + " of " + expected + ".";
} else {
result.message = "Expected " + actual + " to be within " + within + " of " + expected + ", but was within " + diff + ".";
}
return result;
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment