-
-
Save mathieul/966776 to your computer and use it in GitHub Desktop.
/* | |
* jasmine.requirejs() returns a function that will load the file(s) required | |
* and will wait until it's done before proceeding with running specs. | |
* The function returned is intended to be passed to beforeEach() so the file(s) | |
* is(are) loaded before running each spec. | |
* | |
* Syntax: | |
* | |
* jasmine.requirejs(options, files) | |
* or | |
* jasmine.requirejs(files) | |
* | |
* Where: | |
* - options is a configuration object like the one you | |
* can bass to RequireJS require() function (i.e.: baseUrl, paths, etc...) | |
* and | |
* - files is an array of files to require (i.e.: ['lib/common', 'views/main']) | |
* | |
* Example: | |
* | |
* beforeEach(jasmine.requirejs({ | |
* baseUrl: '/public/javascripts' | |
* }, [ | |
* 'lib/app.help-mgr' | |
* ])); | |
*/ | |
jasmine.requirejs = function () { | |
var params = Array.prototype.slice.call(arguments), | |
isLoaded = false, | |
files; | |
files = arguments[0].length ? arguments[0] : arguments[1]; | |
files = files.join(', '); | |
return function () { | |
require.apply(null, params.concat(function () { | |
isLoaded = true; | |
})); | |
waitsFor(function () { | |
return isLoaded; | |
}, 'file(s) to be required/loaded: ' + files); | |
}; | |
}; |
It was not a requirement of the project I wrote this for. All the files included would export their public interface into the application global (window.APP).
This "export their public interface into the application global (window.APP)" is the regular way of work of requireJS? o do you have to do something special when you creates the module definitions? I new to requireJS, and your comment really lost me :)
I don't know the best practices in using require.js. I understand it is possible to have your modules export their public API/objects, so when you include them using require() or define() you can actually use the handles that are passed as parameters to those functions. What I was saying is that for our project, instead of doing that, each file would store on a global object whatever they wanted to expose.
So this snippet doesn't allow you to pass module handles to your jasmine specs. Instead it expects the jasmine specs to test the module through their public API in the global object.
For instance if we test a backbone model Post, in post.js, we would have something like:
define([
'vendor/jquery.min.js',
'vendor/underscore.min.js',
'vendor/backbone.min.js'
], function () {
var Post = Backbone.Model.extend({
...
});
window.APP.Models.Post = Post;
});
and we would test it like so:
describe('a new post', function () {
beforeEach(jasmine.requirejs({
baseUrl: '/public/javascripts'
}, [
'models/post.js'
]));
it('has a name', function () {
var post = new window.APP.Models.Post({name: 'blah'});
expect(post.get('name')).toEqual('blah');
});
});
Now with the code examples I get it. Thanks for taking the time to post them
But how do you get a handle for the module required?