2009-12-07 00:00:00 +0000 by Alex R. Young

JsHamcrest is a library for declaratively expressing matching rules based on matcher objects. This
could be used for testing frameworks, mocking libraries, UI validation
rules and object querying.

If you want to run the tests, you need to install
Fabric and minify the library:

git clone git://github.com/danielfm/jshamcrest.git
fab build
fab pack

Then open test/testSuite.html in your browser. It's also
worth building the documentation (get
Sphinx for this to work):

cd doc
make html

Here are a few examples of the library in use (I tried these in Rhino):

js> var odd = JsHamcrest.Matchers.odd()
js> odd.matches(11)
js> odd.matches(10)

You can also make the matchers globally accessible by copying it into
the current object:


Multiple calls can be chained and then matched like this:

js> JsHamcrest.Integration.copyMembers(this);
js> between(5).and(10).matches(7);
js> filter([1, 2, 3, 4], greaterThan(2))

The library comes with plenty of matchers and also allows you to define
custom matchers. Here is an example of a custom matcher from the

var theAnswerToLifeTheUniverseAndEverything = function() {
    return new JsHamcrest.SimpleMatcher({
        matches: function(actual) {
            return actual == 42;

        describeTo: function(description) {
            description.append('the answer to life, the universe, and everything');