Fine‑Tune Linting on a Per‑File Basis with ESLint Overrides
ESLint overrides let you keep one config while relaxing rules for tests or legacy files. See a practical example, verification commands, and trade-offs for deciding when to use them.
29 Oct 2025, 18:13 UTC

Problem: One Size Does Not Fit All
In a typical JavaScript project, the linting rules you enforce for production code differ from those you want to apply in test suites, stories, or legacy modules. For example, developers often log to the console in unit tests or use loose equality checks in snapshot tests. If you keep a single, strict rule set, these conveniences become a source of noise and can slow down the feedback loop.
At the same time, duplicating configuration files for each context is error‑prone and hard to maintain. The question becomes: how can we keep a single, DRY .eslintrc while allowing file‑specific rule allowances?
Thesis: Use ESLint Overrides to Keep One Config, Many Rules
ESLint’s overrides feature solves this problem elegantly. An overrides array in your root config lets you specify glob patterns and a rule set that applies only to the matched files. The rest of the codebase continues to be linted with the global rules. This keeps the configuration maintainable, reduces duplication, and avoids accidental rule leakage.
Understanding the Overrides Syntax
files– Glob pattern(s) that match the files the override applies to.excludedFiles– Optional glob pattern(s) to exclude from the override.rules– Object containing rule overrides for the matched files.- Other keys (e.g.,
env,parserOptions) can also be set to tailor the environment for those files.
Overrides are evaluated in the order they appear in the array. If multiple overrides match a file, the later ones override earlier ones. This deterministic ordering helps avoid accidental conflicts.
Concrete Example: Relaxing Rules in Jest Tests
Suppose your project uses Jest for unit testing. You want to allow console.log statements and == comparisons in test files, but keep them forbidden in production code.
{
"env": {
"browser": true,
"es2021": true,
"node": true,
"jest": true
},
"extends": ["eslint:recommended", "plugin:react/recommended"],
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"rules": {
"no-console": "error",
"eqeqeq": "error"
},
"overrides": [
{
"files": ["**/*.test.js", "**/*.spec.js"],
"rules": {
"no-console": "off",
"eqeqeq": "off"
}
},
{
"files": ["legacy/**/*.js"],
"rules": {
"no-console": "off",
"no-var": "off"
}
}
]
}
Run eslint . --quiet from the project root. Production files will still error on console.log and ==, while test files will be lint‑free for those rules. Use eslint . --print-config path/to/file.test.js to inspect the effective config and confirm that the overrides took effect.
Trade‑Offs and Limitations
- Fragmented Rule Sets – Overusing overrides can make it hard to see the overall linting posture. Keep the number of overrides reasonable and document their purpose.
- Pattern Confusion – A typo in a glob pattern can cause a file to be linted with the wrong rule set. Test the patterns with
eslint --print-configand double‑check the output. - Performance Impact – ESLint must evaluate each file against every override pattern. In very large codebases, a huge
overridesarray can marginally increase lint time. - Masking Bugs – If you turn a rule off in a test file, you might miss a bug that would surface in production. Use overrides sparingly and document why a rule is relaxed.
- CI Consistency – Ensure that the CI environment runs ESLint with the same config and Node version to avoid environment‑specific differences.
Actionable Checklist for Teams
- Identify Contexts – List file patterns that need different linting (tests, docs, legacy).
- Define Override Rules – For each context, specify the rule changes needed.
- Implement in a Single
.eslintrc– Add anoverridesentry per context. - Verify Locally – Run
eslint . --quietandeslint . --print-configon representative files. - CI Integration – Add a lint step in your pipeline:
eslint --max-warnings 0 .to catch any unexpected warnings. - Document – Add a comment in the config explaining each override’s purpose for future maintainers.
By following this approach, you keep your linting configuration clean, maintainable, and context‑aware—without scattering rule sets across multiple files or hard‑coding exceptions.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.