Skip to content

Instantly share code, notes, and snippets.

@felixflores
Created October 14, 2011 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixflores/1287206 to your computer and use it in GitHub Desktop.
Save felixflores/1287206 to your computer and use it in GitHub Desktop.
// Assuming you have underscore and jQuery as a project dependency.
// You can use this to include individual files on your jasmine specs.
// Sample Usage
requireJS('myJSfile', 'myOtherJsFile');
describe("My stuff", function() {
it("rocks", function() {
// your spec
});
});
// Felix Flores (leaving this comment so that it does not
// get confused with http://requirejs.org/)
function requireJS() {
var files = _.map(Array.prototype.slice.call(arguments), function(file) {
return "public/javascripts/" + file + ".js";
});
var script = document.createElement('script');
script.type = 'text/javascript';
_.each(files, function(file) {
script.src = file;
$('head').append(script);
});
};
@searls
Copy link

searls commented Oct 14, 2011

Snap reaction

I'm struggling to understand the motivation driving this. I only have a couple guesses, but I may be wrong:

  1. Loading all of the JS would explode, so you want to only load in a little at a time (since you're on rails, maybe that means src "*/.{js|coffee}" in your jasmine.yml triggers an error)
  2. You want your specs to identify their own subject code so it's clear what they depend on.

The problem I see with using this for #1 is that when the next spec gets executed, the JS upon which this first spec depended will still be on the page. So as a result, you wouldn't be achieving any more isolation (in fact, if any subsequent specs depend on the same script, that script might be loaded numerous times into the spec runner).

The problem with #2 is that most specs will be named to mirror the directory structure of their subject code, so explicitly declaring that at the top seems redundant.

What I do

Personally, I load all of my sources into my spec runner (prior to any specs loading) whenever I can, because it forces the scripts not to stomp on each other and guarantees me that I'll be able to package them up into a single minified or concatenated script. When I can't do that, I use jasmine.yml (or in Maven, my pom.xml) to explicitly state which sources I need. Are either of those not applicable or not good ideas from your perspective?

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