Skip to content

Instantly share code, notes, and snippets.

@madtrick
Created September 15, 2013 10:37
Show Gist options
  • Save madtrick/6569580 to your computer and use it in GitHub Desktop.
Save madtrick/6569580 to your computer and use it in GitHub Desktop.
Jasmine and RequireJS together. Easy way
require ['fileA', 'fileB'], (A, B) ->
describe "An example", ->
it "depends on A and B"
/*
* Little snippet to make Jasmine and RequireJS play nice together.
*
* */
(function(){
var originalRequire = require;
var originalJasmineExecute = jasmine.getEnv().execute;
var requiresCounter = 0;
require = function (deps, callback){
requiresCounter++;
var newCallback = function(){
requiresCounter--;
callback.apply(this, Array.prototype.slice.call(arguments));
if (requiresCounter === 0){
// Trigger jasmine execution
originalJasmineExecute.call(jasmine.getEnv());
}
}
originalRequire.call(this, deps, newCallback);
};
jasmine.getEnv().execute = function(){};
originalRequire.config({baseUrl : "http://localhost:8000/public/assets/"});
})()
@madtrick
Copy link
Author

Setup for using Jasmine (guard-jasmine) and RequireJS.

After looking for a way to use Jasmine and RequireJS together I came out with the following solution which I think is simpler compared to others.

Steps:

  1. In jasmine.yml require only the requirejs file as src_files. For example:

    src_files:
        - public/assets/require.js
    
  2. Create and include the requirejs_helper (included above). Customize requirejs to your needs:

    • Set a baseUrl.
    • Set needed shims.
    • Etc.

    This helpers keeps count of the required files and only starts the execution of Jasmine when all these files are loaded.

After this simple steps write your specs and enjoy

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