The first thing to do is download the JsHamcrest source file. You can find the download links at the index page.
To be able to use JsHamcrest inside the browser, all you have to do is link the source file from within your HTML file. For example, create a new file with the following content and open it on your web browser of choice:
<html>
<header>
<title>Page title</title>
<script type="text/javascript" src="path/to/jshamcrest.js"></script>
<script type="text/javascript">
var odd = JsHamcrest.Matchers.odd();
alert(odd.matches(11)); // Expected: true
</script>
</header>
<body>
</body>
</html>
If an alert message “true” pops up when you open the page, then congratulations!
Note
Don’t forget to change the 4th line to make it point to your actual JsHamcrest source file.
Since JsHamcrest doesn’t depend on any browser-specific JavaScript features (like document, window, and so on), you should be able to use it with any modern stand-alone JavaScript interpreter.
The snippet below shows how to reproduce the previous example using Rhino, a Java-based JavaScript interpreter/compiler developed by Mozilla:
js> load('path/to/jshamcrest.js')
js> var odd = JsHamcrest.Matchers.odd()
js> odd.matches(11)
true
js> odd.matches(10)
false
On the previous example, you saw the JsHamcrest.Matchers.odd() function, which returns a matcher object that checks whether the given number is odd. In other words, a matcher is an object that determines whether two things are equivalent.
Now try to figure out what the following matchers do:
// Make JsHamcrest matchers globally accessible
JsHamcrest.Integration.copyMembers(this);
equalTo('10').matches(10); // Expected: true
between(5).and(10).matches(7); // Expected: true
greaterThan(Math.PI).matches(4); // Expected: true
To make things easier, try to read each statement backwards. For instance:
It’s not that hard after all, huh?
See also
JsHamcrest.Matchers namespace for the complete list of matchers.