JsHamcrest.Integration — Third-Party Integration Utilities

Provides functions to make it easy to integrate JsHamcrest with other popular JavaScript frameworks.

JsHamcrest.Integration.copyMembers([source], target)

Copies all members of an object to another.

Parameters:
Returns:

Nothing.

JsHamcrest.Integration.installMatchers(matchersNamespace)

Copies all members of matchersNamespace to JsHamcrest.Matchers.

Parameters:matchersName – Namespace that contains the matchers to be installed.
Returns:Nothing.
JsHamcrest.Integration.installOperators(operatorsNamespace)

Copies all members of operatorsNamespace to JsHamcrest.Operators.

Parameters:operatorsNamespace – Namespace that contains the operators to be installed.
Returns:Nothing.

Unit Testing Frameworks

JsHamcrest is perfect to enhance your JavaScript testing code.

Dummy Functions For Easy Prototyping

JsHamcrest.Integration.WebBrowser()

Uses the web browser’s alert() function to display the assertion results. Great for quick prototyping:

<!-- Activate dummy integration -->
<script type="text/javascript" src="jshamcrest.js"></script>
<script type="text/javascript">
    JsHamcrest.Integration.WebBrowser();

    var calc = new MyCalculator();
    assertThat(calc.add(2,3), equalTo(5));
</script>
Returns:Nothing.
JsHamcrest.Integration.Rhino()

Uses the Rhino’s print() function to display the assertion results. Great for quick prototyping:

js> load('jshamcrest.js')
js> JsHamcrest.Integration.Rhino();
js>
js> var calc = new MyCalculator();
js> assertThat(calc.add(2,3), equalTo(5));
[SUCCESS] Expected equal to 5: Success
Returns:Nothing.

Jasmine – BDD for your JavaScript

JsHamcrest.Integration.jasmine({scope})

Integrates JsHamcrest with Jasmine.

The following code is an example on how to set up your project:

<!-- Jasmine dependencies -->
<link rel="stylesheet" type="text/css" href="jasmine.css">
<script type="text/javascript" src="jasmine.js"></script>
<script type="text/javascript" src="jasmine-html.js"></script>

<!-- Activate Jasmine integration -->
<script type="text/javascript" src="jshamcrest.js"></script>
<script type="text/javascript">
    JsHamcrest.Integration.jasmine();

    // Same as above
    JsHamcrest.Integration.jasmine({
        scope: this
    });

    describe('Calculator', function() {
        var calc;

        beforeEach(function() {
            calc = new MyCalculator();
        });

        it('should add two numbers', function() {
            assertThat(calc.add(2,3), equalTo(5));
        });
    });
</script>

<script type="text/javascript">
   jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
   jasmine.getEnv().execute();
</script>
Parameters:scope(Optional, default=this) Copies all matchers to the given scope.
Returns:Nothing.

JsTestDriver – Remote JavaScript Console

JsHamcrest.Integration.JsTestDriver({scope})

Integrates JsHamcrest with JsTestDriver. Instructions on how to set up your project:

  1. Let’s assume your project root directory have a lib directory to keep your project’s dependencies. In this case, copy the jshamcrest.js file to that directory;

  2. Create a file plugin/jshamcrest-plugin.js in your project root directory and put one of the following lines inside it:

    JsHamcrest.Integration.JsTestDriver();
    
    // Same as above
    JsHamcrest.Integration.JsTestDriver({
        scope:this
    });
    
  3. Finally, edit the jsTestDriver.conf file as follows:

    load:
      - lib/*.js
      - <source directory>
      - <test cases directory>
      - plugin/*.js
    

That’s it. Your test cases should now have access to JsHamcrest functions:

CalculatorTest = TestCase('CalculatorTest');

CalculatorTest.prototype.testAdd = function() {
    var calc = new MyCalculator();
    assertThat(calc.add(2,3), equalTo(5));
};
Parameters:scope(Optional, default=this) Copies all matchers to the given scope.
Returns:Nothing.

JsUnitTest – JavaScript Unit Testing Framework

JsHamcrest.Integration.JsUnitTest({scope})

Integrates JsHamcrest with JsUnitTest.

The following code is an example on how to set up your project:

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

<!-- Activate JsUnitTest integration -->
<script type="text/javascript" src="jshamcrest.js"></script>
<script type="text/javascript">
    JsHamcrest.Integration.JsUnitTest();

    // Same as above
    JsHamcrest.Integration.JsUnitTest({
        scope: JsUnitTest.Unit.Testcase.prototype
    });
</script>

<script type="text/javascript">
    new Test.Unit.Runner({
        setup: function() {
        },

        tearDown: function() {
        },

        testAdd: function() { with(this) {
            var calc = new MyCalculator();
            assertThat(calc.add(2,3), equalTo(5));
        }}
    }, {'testLog':'myLog'});
</script>
Parameters:scope(Optional, default=JsUnitTest.Unit.Testcase.prototype) Copies all matchers to the given scope.
Returns:Nothing.

jsUnity – Lightweight JavaScript Testing Framework

JsHamcrest.Integration.jsUnity({scope, attachAssertions})

Integrates JsHamcrest with jsUnity.

The following code is an example on how to set up your project:

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

<!-- Activate jsUnity integration -->
<script type="text/javascript" src="jshamcrest.js"></script>
<script type="text/javascript">
    function CalculatorTestSuite() {
        function testAdd() {
            var calc = new MyCalculator();
            assertThat(calc.add(2,3), equalTo(5));
        }
    }

    // Activate the jsUnity integration
    JsHamcrest.Integration.jsUnity();

    // Same as above
    JsHamcrest.Integration.jsUnity({
        scope: jsUnity.env.defaultScope,
        attachAssertions: false
    });

    var results = jsUnity.run(CalculatorTestSuite);
</script>
Parameters:
  • scope(Optional, default=jsUnity.env.defaultScope) Copies all matchers to the given scope.
  • attachAssertions(Optional, default=false) Whether JsHamcrest should also copy jsUnity’s assertion functions to the given scope.
Returns:

Nothing.

QUnit – JavaScript Test Suite

JsHamcrest.Integration.QUnit({scope})

Integrates JsHamcrest with QUnit.

The following code is an example on how to set up your project:

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

<!-- Activate QUnit integration -->
<script type="text/javascript" src="jshamcrest.js"></script>
<script type="text/javascript">
    JsHamcrest.Integration.QUnit();

    // Same as above
    JsHamcrest.Integration.QUnit({
        scope: this
    });

    $(document).ready(function(){
        test('Calculator should add two numbers', function() {
            var calc = new MyCalculator();
            assertThat(calc.add(2,3), equalTo(5));
        });
    });
</script>

<!-- QUnit and dependencies -->
<script type="text/javascript" src="testrunner.js"></script>
Parameters:scope(Optional, default=this) Copies all matchers to the given scope.
Returns:Nothing.

screw-unit – JavaScript BDD Framework

JsHamcrest.Integration.screwunit({scope})

Integrates JsHamcrest with screw-unit.

The following code is an example on how to set up your project:

<!-- screw-unit and dependencies -->
<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script type="text/javascript" src="jquery.fn.js"></script>
<script type="text/javascript" src="jquery.print.js"></script>
<script type="text/javascript" src="screw.builder.js"></script>
<script type="text/javascript" src="screw.matchers.js"></script>
<script type="text/javascript" src="screw.events.js"></script>
<script type="text/javascript" src="screw.behaviors.js"></script>
<link rel="stylesheet" type="text/css" href="screw.css" />

<!-- Activate screw-unit integration -->
<script type="text/javascript" src="jshamcrest.js"></script>
<script type="text/javascript">
    JsHamcrest.Integration.screwunit();

    // Same as above
    JsHamcrest.Integration.screwunit({
        scope: Screw.Matchers
    });

    Screw.Unit(function() {
        describe('Using JsHamcrest assertions in Screw.Unit', function() {
            it('should succeed', function() {
                assertThat(5, between(0).and(10), 'This assertion must succeed');
            });

            it('should fail', function() {
                assertThat([], not(empty()), 'This assertion must fail');
            });
        });
    });
</script>
Parameters:scope(Optional, default=Screw.Matchers) Copies all matchers to the given scope.
Returns:Nothing.

YUITest – JavaScript Unit Testing Framework

JsHamcrest.Integration.YUITest({scope})

Integrates JsHamcrest with YUITest.

The following code is an example on how to set up your project:

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

<!-- Activate YUITest integration -->
<script type="text/javascript" src="jshamcrest.js"></script>
<script type="text/javascript">
    JsHamcrest.Integration.YUITest();

    // Same as above
    JsHamcrest.Integration.YUITest({
        scope: this
    });
</script>

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

        setUp: function() {
        },

        teardown: function() {
        },

        testAdd: function() {
            var calc = new MyCalculator();
            Assert.that(calc.add(2,3), equalTo(5));
        }
    });
</script>
Parameters:scope(Optional, default=this) Copies all matchers to the given scope.
Returns:Nothing.