Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Library of matcher objects for JavaScript, based on the ideas behind Hamcrest.
JavaScript Python CSS Makefile Shell
Fetching latest commit...
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



JsHamcrest is a JavaScript port of Hamcrest, which provides a library of
matcher objects (also known as constraints or predicates) allowing 'match'
rules to be defined declaratively. JsHamcrest integrates out of the box
with JsUnitTest, QUnit and YUITest.

This is an open source project licenced under the terms of the Lesser General
Public License v3.0 and sponsored by Destaquenet Technology Solutions, a
brazilian software development and consultancy startup.


Visit the following URL to download the latest stable version of JsHamcrest
and its API documentation:


In order to use JsHamcrest with a supported JavaScript unit test framework,
just follow the usual installation/configuration instructions of the chosen
framework and plug JsHamcrest to it. Some examples:

- JsUnitTest

    <!-- JsUnitTest and dependencies -->
    <script type="text/javascript" src="jsunittest.js"></script>
    <!-- Don't forget to activate JsUnitTest integration -->
    <script type="text/javascript" src="jshamcrest.js"></script>
    <script type="text/javascript">

    <!-- Some code... -->

    <script type="text/javascript">
        new Test.Unit.Runner({
            setup: function() {
                this.calc = new MyCalculator();

            teardown: function() {

            testAdd: function() { with(this) {
                var result = calc.add(2, 3);
                assertThat(result, equalTo(5));

            // More tests here...
        }, {'testLog':'myLog'});

- QUnit

    <!-- QUnit and dependencies -->
    <script type="text/javascript" src="jquery.js"></script>

    <!-- Don't forget to activate QUnit integration -->
    <script type="text/javascript" src="jshamcrest.js"></script>
            var calc = new MyCalculator();

            test("Calculator should add two numbers", function() {
                var result = calc.add(2, 3);
                assertThat(result, equalTo(5));

            // More tests here...

    <!-- Some code... -->

    <script type="text/javascript" src="testrunner.js"></script>

- YUITest

    <!-- YUITest and dependencies -->
    <script type="text/javascript" src="assets/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="assets/yuilogger/logger.js"></script>
    <script type="text/javascript" src="assets/yuitest/yuitest.js"></script>

    <!-- Don't forget to activate YUITest integration -->
    <script src="jshamcrest.js" type="text/javascript"></script>
    <script type="text/javascript">

    <!-- Some code... -->

    <script type="text/javascript">
        CalculatorTestCase = new YAHOO.tool.TestCase({
            name: "Calculator test case",

            setup: function() {
                this.calc = new MyCalculator();

            teardown: function() {

            // In YUITest, the assertion method is Assert.that(), not assertThat()!
            // JsHamcrest tries not to screw with the conventions adopted by the
            // unit test framework in use, making the integration feel less
            // intrusive and more natural.

            testAdd: function() {
                var result = this.calc.add(2, 3);
                Assert.that(result, equalTo(5));
            testSub: function() {
                var result = this.calc.sub(2, 3);
                Assert.that(result, equalTo(-1));

            // More tests here...


If you want to build JsHamcrest yourself, you have to install Apache Ant in
your machine if you don't have it already. Visit the following URL to
download Ant and get further instructions on how to install it in your system:

After that, execute the following command line to perform the complete build:

    $ ant all

Doing this, the .js files built will be available inside the 'build' directory.
The API documentation will be available inside the 'doc/api' directory.

Thanks to...

    * Hamcrest:   <>
    * JsUnitTest: <>
    * YUITest:    <>


    Author:  Daniel Fernandes Martins <>
    Company: Destaquenet Technology Solutions <>

Something went wrong with that request. Please try again.