Somewhat strangley this isn't basic functionality
You can add a custom matcher like this:
JasmineExtensions.js
yourGlobal.addExtraMatchers = function () {
var addMatcher = function (name, func) {
func.name = name;
jasmine.matchers[name] = func;
};
addMatcher("toBeGreaterThanOrEqualTo", function () {
return {
compare: function (actual, expected) {
return {
pass: actual >= expected
};
}
};
}
);
};
In effect you're defining a constructor for your matcher - it's a function that returns a matcher object.
Include that before you 'boot'. The basic matchers are loaded at boot time.
Your html file should look like this:
<!-- jasmine test framework-->
<script type="text/javascript" src="lib/jasmine-2.0.0/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-2.0.0/jasmine-html.js"></script>
<!-- custom matchers -->
<script type="text/javascript" src="Tests/JasmineExtensions.js"></script>
<!-- initialisation-->
<script type="text/javascript" src="lib/jasmine-2.0.0/boot.js"></script>
Then in your boot.js add the call to add the matchers after jasmine has been defined but before jasmine.getEnv(). Get env is actually a (slightly misleadingly named) setup call.
The matchers get setup in the call to setupCoreMatchers in the Env constructor.
/**
* ## Require & Instantiate
*
* Require Jasmine's core files. Specifically, this requires and attaches all of Jasmine's code to the `jasmine` reference.
*/
window.jasmine = jasmineRequire.core(jasmineRequire);
yourGlobal.addExtraMatchers();
/**
* Since this is being run in a browser and the results should populate to an HTML page, require the HTML-specific Jasmine code, injecting the same reference.
*/
jasmineRequire.html(jasmine);
/**
* Create the Jasmine environment. This is used to run all specs in a project.
*/
var env = jasmine.getEnv();
They show another way of adding custom matchers in the sample tests, however the way it works is to recreate the matcher(s) before every single test using a beforeEach
. That seems pretty horrible so I thought I'd go with this approach instead.