Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

JSHint with Jasmine 2

I’ve been using Jasmine 2 on a recent project and I wanted add JSHint as part of our Jasmine build. On a previous project I had used Brandon Keeper’s approach and started there. The core of his solution works with Jasmine 2, with some slight changes. Here is an example:

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();"GET", path, false);
    } catch (e) {
      throw new Error("couldn't fetch " + path + ": " + e);
    if (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)) {

    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

(gist is here)

Add a spec file similar to this next to your other Jasmine specs and you should see any linting errors when you run your build. The main changes from Brandon’s example are around how you tell Jasmine about a linting error. If you use an approach like this, the things you are likely to need to change are the regex for your source and spec files as well as the options used by JSHint.

Happy linting!

  1. Alex Kwiatkowki says:

    Are you using the jasmine-gem and does this config lint the files that application.js requires? If so, I’d be interested in what your Rails config for the test environment looks like?

    By default it appears the jasmine-gem loads each of the sprocket requires in a separate script tag so this regex won’t match them.

  2. Hunter Gillane says:

    Hey Alex –

    Currently you would have to update the regex to include the individual files you want linted. This weekend I am going to look at what it takes to make it aware of jasmine-gem config as well as pulling the JSHint config into a separate file.

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *