Skip to content

Instantly share code, notes, and snippets.

@EndangeredMassa
Created April 22, 2012 06:00
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 EndangeredMassa/2459830 to your computer and use it in GitHub Desktop.
Save EndangeredMassa/2459830 to your computer and use it in GitHub Desktop.
Jasmine and RequireJS
TEST_ENV = (typeof window['jasmine'] == 'object')
prefix = if TEST_ENV then '/public' else ''
require.config
baseUrl: prefix
#!/usr/bin/env coffee
CSSOM = require 'cssom'
fs = require 'fs'
if process.argv.length != 4
console.log('Usage: hidey inputFile outputFile')
return
fileName = process.argv[2]
outputFileName = process.argv[3]
input = fs.readFileSync(fileName).toString()
parsed = CSSOM.parse(input)
output = ''
for rule in parsed.cssRules
output += "#{rule.selectorText} { display: none; }\r\n" if rule.style['display'] == 'none'
fs.open outputFileName, 'w', '0666', ( e, id ) ->
fs.write id, output, null, 'utf8', ->
fs.close id, ->
src_files:
- public/javascripts/require.min.js
- public/javascripts/bootstrap.js
spec_files:
- '**/*[sS]pec.js'
# contrived example, for sure
describe 'Order View', ['OrderView'], ('OrderView') ->
it 'works', ->
new OrderView
it 'interacts with other modules', ['LineItemView'], (LineItemView) ->
order = new OrderView
order.addItem(new LineItemView)
results = order.process()
expect(results.owed).toBe(0)
(function(){
// save references to the original global functions
var _it = window.it;
var _describe = window.describe;
// override the global `it` function
window.it = function(description, depsOrTestFn, testFn){
// the middle argument here represents either the dependency list or the actual spec definition
if(arguments.length === 1) {
// call the original pending spec method
window.xit.call(this, description);
} else if(arguments.length === 3) {
// use our fancy new RequireJS method
// we actually still use the old method, but setup some conditions before our real spec runs
_it(description, function() {
jasmineContext = this;
var readyModules = [];
// here we wait for our modules to be required before continuing
waitsFor(function() {
require(depsOrTestFn, function() {
readyModules = arguments;
});
return readyModules.length === depsOrTestFn.length;
});
// now that our modules are loaded, we can pass them along to the original spec definition
runs(function() {
var arrayOfModules;
arrayOfModules = Array.prototype.slice.call(readyModules);
testFn.apply(jasmineContext, arrayOfModules);
});
});
} else {
// if we only have 2 arguments, just use the old functionality
_it.apply(this, [description, depsOrTestFn]);
}
};
// save a reference to the current window.onload (jasmine suite runner)
window._onload = window.onload;
// stub out the call so that we can wait for our specs to be registered
window.onload = function(){};
// set up a counter so that we know when we're done registering
window._describeCount = 0;
// override the global `describe` method
window.describe = function(description, depsOrTestFn, testFn) {
if(arguments.length === 1) {
// same deal, call the pending describe method if we have 1 argument
window.xdescribe.call(this, description);
} else if(arguments.length === 3) {
// immediately increase our counter
_describeCount++;
var readyModules = [];
// require our modules
require(depsOrTestFn, function() {
readyModules = arguments;
// call the original method
// this works different from our it method because
// we can't use waitsFor in this context
_describe(description, function(){
testFn.apply(this, readyModules);
//descrease our counter once we'ved registered this describe block
_describeCount--;
});
// if we're done, call the original onload (which runs the jasmine suite)
if(_describeCount <= 0) {
_onload();
}
});
} else {
// otherwise call the original describe
_describe.call(this, description, depsOrTestFn);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment