Skip to content

Instantly share code, notes, and snippets.

@searls
Created June 21, 2011 22:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save searls/1039164 to your computer and use it in GitHub Desktop.
Save searls/1039164 to your computer and use it in GitHub Desktop.
JSHint spec for use w/ jasmine-maven-plugin (where vendor sources are in a folder called 'vendor' and excluded).
describe('JSHint', function() {
var options = {
curly : true,
white : false,
evil: true,
indent : 2
}, excludes = /^.*\/vendor\/.*$/, includes = /^.*js$/;
function get(path) {
path = path + "?" + new Date().getTime();
var xhr;
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;
}
_.each(document.getElementsByTagName('script'), function(element) {
var script = element.getAttribute('src');
if (!includes.test(script) || excludes.test(script)) {
return;
}
var source;
try {
source = get(script);
} catch (e) {
//Oh well. Probably an XMLHttpRequest same-origin policy violation
}
if (source !== undefined) {
it(script, function() {
var self = this;
var result = JSHINT(source, options);
_.each(JSHINT.errors, function(error) {
self.addMatcherResult(new jasmine.ExpectationResult({
passed: false,
message: "line " + error.line + ' - ' + error.reason + ' - ' + error.evidence
}));
});
expect(true).toBe(true); // force spec to show up if there are no errors
});
}
});
});
@searls
Copy link
Author

searls commented Oct 26, 2011

Credit where it's due, I adapted this from @bkeepers site. http://opensoul.org/blog/archives/2011/02/19/jslint-and-jasmine/

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