secret
Last active

JSHint with Jasmine 2

  • Download Gist
jasmine2-js-hint.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
describe('JSHint', function () {
var options = {curly: true, white: true, indent: 2},
files = /^\/assets\/application\.js|.*spec\.js$/;
 
function get(path) {
path = path + "?" + new Date().getTime();
 
var xhr;
try {
xhr = new 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 (!files.test(script)) {
return;
}
 
it(script, function () {
var env = jasmine.getEnv();
var source = get(script);
var result = JSHINT(source, options);
 
_.each(JSHINT.errors, function (error) {
env.currentSpec.addExpectationResult(false, {
passed: false,
message: "line " + error.line + ' - ' + error.reason + ' - ' + error.evidence
});
});
expect(true).toEqual(true);
});
});
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.