Saturday, August 30, 2014

Using JSHint with the Jasmine test framework

If you develop JavaScript code and use the Jasmine test framework to test your code, you probably have found that you get a lot of warnings in the test specs (such as blahBlahSpec.js) when checking your code with JSHint.
It turns out there is a fairly simple way to turn off all those warnings that relate to globals introduced by Jasmine.  The predefined globals can simply be added to the .jshintrc config file under the "predef" option. For example:
{
  "bitwise":     true,
  "camelcase":   true,
  "curly":       true,
  "immed":       true,
  "latedef":     true,
  "newcap":      true,
  "noarg":       true,
  "noempty":     true,
  "quotmark":    "single",
  "undef":       true,
  "unused":      true,
  "strict":      true,
  "debug":       true,
  "evil":        false,
  "node":        true,
  "predef": [
    "describe",
    "expect",
    "it",
    "afterEach",
    "beforeEach",
    "spyOn",
    "xdescribe",
    “xit”
]
}

The .jshintrc file can reside in the top level of your project or in a parent directory, since JSHint will search up the directory tree for that file.  Happy code checking!
References:
1. Introduction to the Jasmine test framework.
2. JSHint configuration documentation.