Skip to content

Instantly share code, notes, and snippets.

@sinedied
Forked from toddb/gist:1474205
Last active December 20, 2015 03:28
Show Gist options
  • Save sinedied/6063464 to your computer and use it in GitHub Desktop.
Save sinedied/6063464 to your computer and use it in GitHub Desktop.
/**
* @license Copyright (c) 2012, toddb GoneOpen Limited.
* Available via the MIT or new BSD license.
* based on https://gist.github.com/966776 (mathieul) and forked to https://gist.github.com/1474205
*
* 23/07/13: - added automatic test scoping of callback function arguments.
* - renamed _requires alias to jrequire
*
* 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, [callback])
* alias: jrequire([options,] files, [callback])
*
* 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'])
* - callback is the named parameters as you get from requirejs (ie function(common, main){})
*
* Examples:
*
* Loading either using jasmine.requirejs or via alias jrequire
*
* beforeEach(jasmine.requirejs({
* baseUrl: '/public/javascripts'
* },
* ['lib/app.help-mgr']
* ));
*
* or
*
* beforeEach(jrequire(['lib/app.help-mgr']));
*
*
* Loading using alias jrequire and getting access to the return functions
*
* (using the arguments passed through)
* beforeEach(jrequire(['underscore'], function(){ _ = arguments[0]}));
*
* or
*
* (using explicit arguments, automatically scoped for the tests)
* beforeEach(jrequire(['underscore'], function( _ ){ })); // you can now use the '_' prefix in your tests
*
* Additional Option 1: you can set the require config for multiple requires
*
* jasmine.requirejs.config = {
* BaseUrl: '/public/javascripts'
* }
*
*/
jrequire = jasmine.requirejs = function () {
// get function parameter names
function getParamNames(func) {
var funStr = func.toString();
funStr = funStr.replace('/((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg', ''); // strip comments
return funStr.slice(funStr.indexOf('(') + 1, funStr.indexOf(')')).match(/([^\s,]+)/g);
}
// TODO: be able to hand in just a string for the require
var params = Array.prototype.slice.call(arguments),
isLoaded = false,
files;
// if the last argument is a callback then grab it for calling later
var cb = function () { }
if (typeof params[params.length - 1] == 'function') {
cb = params.pop()
}
/* load up a global setting for the require config */
if (params.length == 1) params.unshift({})
params[0] = $.extend(true, {}, jasmine.requirejs.config, params[0])
files = arguments[0].length ? arguments[0] : arguments[1];
files = files.join(', ');
return function () {
require.apply(null, params.concat(function () {
cb.apply(null, arguments)
isLoaded = true;
// expose callback function arguments as variables for the tests
var paramNames = getParamNames(cb);
for (i = 0, len = params.length; i < len; ++i)
this[paramNames[i]] = arguments[i];
}));
waitsFor(function () {
return isLoaded;
}, 'file(s) to be required/loaded: ' + files);
};
};
jasmine.requirejs.config = {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment