Created
March 24, 2012 18:14
-
-
Save damienalexandre/2185795 to your computer and use it in GitHub Desktop.
Jasmine Spec which run a list of files against JSHint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This spec run some files against JSHint | |
* | |
* Inspired by https://github.com/bkeepers/lucid/blob/master/spec/javascripts/z_jshint_spec.js | |
* @author bkeepers | |
* @author dalexandre | |
* | |
* @require JSHint, jQuery | |
*/ | |
describe('JSHint', function () | |
{ | |
var options = { curly: true, white: false, indent: 2 }; | |
/** | |
* Get a list of file to check against JSHint | |
*/ | |
function getFiles() | |
{ | |
return [ | |
"./web_root/js/modules/Bootstrap.js", | |
"./web_root/js/modules/Pony.js", | |
"./web_root/js/modules/SuperPony.js", | |
"./web_root/js/modules/MegaPony.js" | |
]; | |
// or play with $('script') and build a list dynamicaly! | |
} | |
/** | |
* Fetch a file via XmlHttpRequest | |
* | |
* @param path | |
* @return the file content | |
*/ | |
function fetch(path) | |
{ | |
var xhr; | |
path = path + "?" + new Date().getTime(); | |
try | |
{ | |
xhr = new jasmine.XmlHttpRequest(); | |
xhr.open("GET", path, false); | |
xhr.send(null); | |
} | |
catch (e) | |
{ | |
throw new Error("Couldn't fetch " + path + ": " + e); | |
} | |
if (xhr.status < 200 || xhr.status > 299) | |
{ | |
throw new Error("Could not load '" + path + "'."); | |
} | |
return xhr.responseText; | |
} | |
/** | |
* Create a Jasmine Spec by file | |
*/ | |
$.each(getFiles(), function (index, script) | |
{ | |
it(script, function() | |
{ | |
var self = this, | |
source = fetch(script), | |
result = JSHINT(source, options); | |
$.each(JSHINT.errors, function (index, error) | |
{ | |
self.addMatcherResult(new jasmine.ExpectationResult({ | |
passed: false, | |
message: "line " + error.line + ' - ' + error.reason + ' - ' + error.evidence | |
})); | |
}); | |
// force spec to show up if there are no errors | |
expect(true).toBe(true); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment