Skip to content

Instantly share code, notes, and snippets.

@erikfried
Created July 1, 2011 09:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikfried/1058192 to your computer and use it in GitHub Desktop.
Save erikfried/1058192 to your computer and use it in GitHub Desktop.
Testing YUI3 modules with Jasmine
YUI.add("my-module", function (Y) {
Y.namespace("mynamespace");
Y.mynamespace.MyModule = function () {
return {hello : "hello"};
};
}, '0.0.1' /*version*/, {
requires : ["base"]
});
YUI({
allowRollup : true,
modules: {
'my-module' : {
fullpath : 'MyModule.js',
requires : ["base"]
}
}
}).use('my-module', function (Y) {
//Here goes the jasmine tests...
describe('my module', function () {
it("has hello", function () {
var module = new Y.mynamespace.MyModule();
expect(module.hello).toBe("hello");
});
});
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jasmine Test Runner</title>
<link rel="stylesheet" type="text/css" href="lib/jasmine-1.0.2/jasmine.css">
<script type="text/javascript" src="lib/jasmine-1.0.2/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-1.0.2/jasmine-html.js"></script>
<!-- Include YUI somehow... -->
<script type="text/javascript" src="http://yui.yahooapis.com/3.4.0pr2/build/yui/yui-debug.js"></script>
<!-- NO NEED TO DECLARE SOURCES,
their locations are specified when creating a YUI instance
and will be loaded with the YUI loader -->
<!-- include spec files here... -->
<script type="text/javascript" src="MyModuleSpec.js"></script>
</head>
<body>
<script type="text/javascript">
jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
//Original implementation:
//jasmine.getEnv().execute();
// Just executing the jasmine fixture synchronously won´t work,
// because our YUI modules will not be ready quite yet.
// Also, wrap up any previously defined onload handler in a closure and invoke it too
window.onload = (function (oldOnLoad) {
return function () {
if (oldOnLoad) {
oldOnLoad();
}
jasmine.getEnv().execute();
};
}(window.onload));
</script>
</body>
</html>
@erikfried
Copy link
Author

Ok, what´s the deal?

The big deal is that the specrunner you get by default from jasmine will run "jasmine.getEnv().execute()" before any tests have been added. The adding of tests happens within the YUI.use callback function. This callback will be run before the onload event, so you can just wait with the execution til the onload event fires.

Note that you dont have to specify the sources to your module in the specrunner html, because they are specficed within the spec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment