Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mocha-eslint

BadgeLabs13.8kMIT7.0.0

run ESLint as mocha tests

mocha, eslint, mochaplugin

readme

mocha-eslint

Travis Build Status Dependency Status NPM version

A simple way to run ESLint in your Mocha tests without a task runner like Grunt or Gulp.

Inspired by mocha-jshint from Allan Ebdrup.

Installation

You can install into your Node.js project as a development dependency with:

npm install --save-dev mocha-eslint

mocha-eslint will install ESLint for itself, so you don't need to worry about adding it to your consuming module.

The same is not true for Mocha. You should already have Mocha installed in your consuming module.

Usage

After mocha-eslint is installed, you can use it by creating a test file for Mocha and requiring mocha-eslint like so:

var lint = require('mocha-eslint');

This will return a function with the signature:

lint(paths, options)

where paths is an array of paths from your project's top level directory (as of v0.1.2, you can also include glob patterns) and options has a single property formatter that can be assigned to the name of any of the ESLint formatters (stylish (the default), compact, checkstyle, jslint-xml, junit and tap) or the full path to a JavaScript file containing a custom formatter. If options is not included, the default "stylish" formatter will be used.

So, a full test file to run in Mocha might look like:

var lint = require('mocha-eslint');

// Array of paths to lint
// Note: a seperate Mocha test will be run for each path and each file which
// matches a glob pattern
var paths = [
  'bin',
  'lib',
  'tests/**/*Test.js',
  '!tests/NotATest.js', // negation also works
];

var options = {
  // Specify style of output
  formatter: 'compact',  // Defaults to `stylish`

  // Only display warnings if a test is failing
  alwaysWarn: false,  // Defaults to `true`, always show warnings

  // Increase the timeout of the test if linting takes to long
  timeout: 5000,  // Defaults to the global mocha `timeout` option

  // Increase the time until a test is marked as slow
  slow: 1000,  // Defaults to the global mocha `slow` option

  // Consider linting warnings as errors and return failure
  strict: true,  // Defaults to `false`, only notify the warnings

  // Specify the mocha context in which to run tests
  contextName: 'eslint',  // Defaults to `eslint`, but can be any string
};

// Run the tests
lint(paths, options);

Notes

This module does not make any decisions about which ESLint rules to run. Make sure your project has a .eslintrc file if you want ESLint to do anything.

changelog

mocha-eslint Changelog

7.0.0

  • [BREAKING] Update to ESLint version 7
  • [BREAKING] Drop support for Node < 10.12.0

6.0.0

  • [BREAKING] Update to ESLint version 6
  • [ENHANCEMENT] Support Mocha 6

5.0.0

  • [BREAKING] Update to ESLint verison 5
  • [BREAKING] Drop support for Node < 6

4.1.0

  • [ENHANCEMENT] Add contextName option to control Mocha report output

4.0.0

  • [BREAKING] Update to ESLint ^4.2.0

3.0.1

  • [BUGFIX] Remove tests from npm package

3.0.0

  • [BREAKING] Update to ESLint 3.0.0

2.1.1

  • [BUGFIX] Prevent error when no options are given

2.1.0

  • [ENHANCEMENT] Support Mocha slow override option
  • [ENHANCEMENT] Add strict option to treat warnings as failures

2.0.2

  • [BUGFIX] Prevent duplicate error messages when more than one file with errors

2.0.1

  • [BUGFIX] Prevent duplicate error messages

2.0.0

  • [ENHANCEMENT] Add multiple globs and negation support
  • [ENHANCEMENT] Add mocha timeout option
  • [BREAKING] Update to ESLint 2.0.0

1.0.0

  • [BREAKING] Update to ESLint 1.0.0

v0.2.0

  • [ENHANCEMENT] Add alwaysWarn option, defaults to true
  • [ENHANCEMENT] Increment ESLint to v0.24.0

v0.1.7

  • [ENHANCEMENT] Increment ESLint to v0.21.0

v0.1.6

  • [ENHANCEMENT] Increment ESLint to v0.20.0

v0.1.5

  • [ENHANCEMENT] Increment ESLint to v0.19.0

v0.1.4

  • [ENHANCEMENT] Increment ESLint to v0.18.0

v0.1.3

  • [BUGFIX] Fix glob dependency

v0.1.2

  • [ENHANCEMENT] Add ability to pass glob patterns as arguments
  • [BUGFIX] Display ESLint warnings even if tests pass

v0.1.1

  • [ENHANCEMENT] Added acceptance tests

v0.1.0

  • [BUGFIX] Removed template strings for Node compatibility
  • [ENHANCEMENT] Added badges to readme