Eli Weinstock-Herman

Unit Testing with Jasmine 2.0 and Require.JS

Original post posted on March 04, 2014 at LessThanDot.com

Jasmine 2.0 has changed how it loads and executes tests, using a boot script now to handle the details. If you try to plug some require() calls into the sample SpecRunner.html page, Jasmine will be done and finished before the require() statement loads the test modules and their dependencies.

The problem is that RequireJS loads the dependencies asynchronously, but the standard boot script for Jasmine runs when window.onload is called. So how do we fix it?

Option 1: Call window.onload Ourselves

One option to solve this is to simply call window.onload again:

<script type="text/javascript" src="tests/lib/jasmine-2.0.0/jasmine.js"></script>
<script type="text/javascript" src="tests/lib/jasmine-2.0.0/jasmine-html.js"></script>
<script type="text/javascript" src="tests/lib/jasmine-2.0.0/boot.js"></script>
<script type="text/javascript" src="lib/require-2.1.8.min.js" data-main="test-main"></script>

<script type="text/javascript">
	// list spec files here
	require([
		"spec/someAwesomeProcess.spec",
		"spec/anotherAwesomeProcess.spec"

	], function () {
		window.onload();
	});
</script>

But that’s icky and causes you to have two test bars across the screen (and probably doesn’t work well with other reporters either).

JasmineDoubleFail

Yeah, that’s special.

Option 2: Custom Boot Script

Or we can fix the root cause, the fact that the tests are running on window.onload and that doesn’t play well with AMD. The boot script included with Jasmine is supposed to be a template that can be customized to your own needs, so let’s take advantage of that. Copying the existing boot script, we can replace the section that registers the tests to onload with one that will add a callable method to the window:

boot-without-onload.js

  /**
   * ## Execution
   *
   * No onload, only on demand now
   */

  window.executeTests = function(){
    htmlReporter.initialize();
    env.execute();
  };

And then update our SpecRunner to include this replacement boot script and require the test files prior to executing the tests:

    <script type="text/javascript" src="tests/lib/jasmine-2.0.0/jasmine.js"></script>
    <script type="text/javascript" src="tests/lib/jasmine-2.0.0/jasmine-html.js"></script>
    <script type="text/javascript" src="tests/lib/jasmine-2.0.0/boot-without-onload.js"></script>
    <script type="text/javascript" src="lib/require-2.1.8.min.js" data-main="test-main"></script>

    <script type="text/javascript">
        // list spec files here
        require([
            "spec/someAwesomeProcess.spec",
            "spec/anotherAwesomeProcess.spec"

        ], function () {
            window.executeTests();
        });
    </script>

And there we go, Jasmine is now working exactly the same as if we were running without RequireJS (and had pasted 500 script tags in the file).

Comments are available on the original post at lessthandot.com