mocha-eslint
raw JSON → 7.0.0 verified Sat Apr 25 auth: no javascript
Run ESLint as Mocha tests without a task runner like Grunt or Gulp. Version 7.0.0 supports ESLint ^7.0.0 and requires Node ^10.12.0 or >=12.0.0. It creates separate Mocha tests for each linted path or glob pattern, with options for formatter, timeout, slow, strict mode, and context name. Unlike alternatives that require plugin registration, this package directly exposes a function to run linting in any Mocha test file.
Common errors
error Error: Cannot find module 'eslint' ↓
cause ESLint is not installed or not installed as a peer dependency.
fix
Run npm install eslint --save-dev
error TypeError: lint is not a function ↓
cause Using named import instead of default import.
fix
Use const lint = require('mocha-eslint');
error Mocha error: timeout of 2000ms exceeded ↓
cause Linting takes longer than the default Mocha timeout.
fix
Set the timeout option in mocha-eslint or globally in Mocha.
Warnings
breaking Version 7.0.0 updates ESLint to ^7.0.0. ESLint 7 includes breaking changes such as removal of some rules and configuration changes. ↓
fix Review ESLint 7 migration guide for rule and config changes.
breaking Version 6.0.0 drops support for Node 6 and updates to ESLint 6 and Mocha 6. ↓
fix Use Node >=8.0.0. Update ESLint config for version 6 changes.
breaking Version 5.0.0 drops Node <6 support and updates ESLint to ^5.10.0. ↓
fix Ensure Node >=6.0.0. Review ESLint 5 migration.
gotcha Mocha must be installed separately; mocha-eslint does not include it. ↓
fix Install mocha as a devDependency: npm install --save-dev mocha.
gotcha Negation patterns (e.g., !tests/NotATest.js) must be at the end of the paths array or they will not work. ↓
fix Place negation patterns after all positive patterns in the array.
Install
npm install mocha-eslint yarn add mocha-eslint pnpm add mocha-eslint Imports
- default wrong
import lint from 'mocha-eslint';correctconst lint = require('mocha-eslint'); - Value
const lint = require('mocha-eslint'); - Type wrong
import { lint } from 'mocha-eslint';correctconst lint = require('mocha-eslint');
Quickstart
const lint = require('mocha-eslint');
const paths = ['src', 'test/**/*.js', '!test/exclude.js'];
const options = {
formatter: 'stylish',
alwaysWarn: true,
timeout: 5000,
slow: 1000,
strict: false,
contextName: 'eslint'
};
lint(paths, options);