Run jshint as Mocha tests with the jshint-stylish reporter.
To install in your node.js project as devDependency, run this command, in the root of your project
npm install mocha-jshint-stylish --save-dev
Mocha defaults to looking for your test specs in the
test folder of your project.
Add this file as
test/jshint.spec.js in your project, with the following content:
That is it you are done.
To grep only the jshint test, just do
mocha --grep jshint
If you are using git as version control you can do the following in your test:
git:modified: truecommits: 2;
This means that jshint will only lint the files that are modified on disk according to git, and the files modified in the last two git commits.
There is also the
git:modified: truecommits: 2masterDiff:true;
This means that if we are on any other branch than
master, only the files changed compared to the
will be linted.
If we are on the
master branch, only the files that are modified on disk according to git, and the files modified in the last
two git commits will be linted.
Normally I would recommend configuring what to lint with
.jshintignore described in
And just lint the working directory.
But if you want to specify specific paths to lint, you can do the following in your test:
Each path may be either a file path or a directory path, and should yield a valid file or directory when passed
path.resolve(). Each path listed in the array will be linted on a separate test.
The default name for the test suite generated by mocha-jshint is jshint, but it may be overridden in the following manner:
title: 'My custom test suite name';
Output the error code at the end of the each message
Set timeout for running the code
choose one of the build-in reporters. Either stylish or simple
or use a custom NPM module or create your own, you can pass the NPM module name or path to a custom reporter file.
In the root of your project you can add a
.jshintignore file, where each line is a file or directory for jshint to ignore
and not check for errors. (see this project for an example)
At the root of your project you can add a
.jshintrc file, that specifies what options you want jshint to run with
(see this project for an example)
You can also add a
.jshintrc file to any subdirectory of your project, to override the .jshintrc settings in the root.
For example in this project I allow some global variables in the
test folder. Global variables that are set when I
run mocha tests. Global variables that are only allowed to be used, in the .js files in the test folder