Using RequireJS with Jasmine 2.0

On June 12, 2014, in Javascript, Testing, by Anuj Gakhar

I’ve been trying to get some Jasmine tests running with RequireJS and although my tests work with Jasmine 1.3.x, I’ve been getting an error if I use Jasmine 2.0 for the tests.

Apparently, Jasmine 2.0 has changed how it loads and executes the tests. If you look at the source here, you’ll see all the tests are executed on window.onload, which is a problem because in your test runner, the require calls to load your tests/specs (and their dependencies) would not be done when window.onload executes. I am not even sure why Jasmine is trying to run the tests on window.onload, it breaks the AMD nature of it.

However, this can be solved with a minor change. In boot.js, you can modify the window.onload to a function that can then be manually called when tests have loaded.

In boot.js :-

[js]// replace window.onload with window.executeJasmineTests
window.executeJasmineTests = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
htmlReporter.initialize();
env.execute();
};[/js]

Then, in your spec runner :-

[js]require(
[
"domReady!",
"spec/todo",
"spec/todoList",
"…other test…."
],
function(document){
window.executeJasmineTests();
}
);[/js]

That should do it for now, until boot.js is made AMD compatible by the Jasmine team.

Tagged with:  

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar
%d bloggers like this: