Fine‑Grained ESLint Linting: Using Overrides to Allow console.log in Tests Only
Use ESLint’s <code>overrides</code> to let <code>console.log</code> in test files while keeping <code>no-console</code> for production code. A practical config, run‑time verification, and common pitfalls are covered.
08 Sept 2025, 00:37 UTC

Why Override Rules for Test Files?
In many projects the no-console rule is enforced to keep production code clean, but test suites often rely on console.log for debugging. ESLint’s overrides field lets you keep the strict rule for source files while relaxing it for test files without touching the base configuration.
How the Override Mechanism Works
The overrides array contains objects that match files via glob patterns. Each object can specify its own rules section. Overrides are processed after the base config, so they can add or remove rules for matched files. If multiple overrides match the same file, the last one in the array wins for any rule that appears in more than one override.
Concrete Example
Assume a project structure:
src/
├─ index.js
└─ utils.test.js
Create .eslintrc.json with the following content:
{
"env": {"browser": true, "node": true, "es2021": true},
"extends": "eslint:recommended",
"rules": {
"no-console": "error"
},
"overrides": [
{
"files": ["**/*.test.js", "**/*.spec.js", "**/*.test.ts", "**/*.spec.ts"],
"rules": {
"no-console": "off"
}
}
]
}
Run ESLint from the project root:
# Check the source file – should flag console.log
eslint src/index.js
# Check the test file – should pass without errors
eslint src/utils.test.js
To verify the effective rule set for a file, use:
eslint --print-config src/utils.test.js
The output will show "no-console": "off" only for the test file.
Common Pitfalls and How to Avoid Them
- Glob Pattern Typos: A small mistake like
**/*.tests.jswill never match*.test.jsfiles. Double‑check spelling and use the--print-configcommand to confirm matches. - Override Ordering: If you add another override that also turns
no-consoleon for**/*.test.js, the last override will override the first. Place the most specific overrides last. - Partial Overrides: Turning off
no-consolein an override only affects that rule. Other rules (e.g.,eqeqeq) still apply unless explicitly overridden. - Global vs. Local: The base rule remains active globally. Accidentally adding
console.logto a production file will still trigger the error.
Limitations
Overrides are evaluated per file, not per directory. If you need different rules for different sub‑folders, include separate override objects with appropriate glob patterns. Also, complex patterns can lead to performance overhead in very large projects.
Practical Checklist
- Define
no-consoleas"error"in the baserulessection. - Add an override that matches all test file patterns and sets
no-consoleto"off". - Run
eslint --print-configon a sample test file to confirm the rule is disabled. - Run
eslint --print-configon a production file to ensure the rule remains enabled. - Commit the configuration and add a linting step to your CI pipeline.
By following this pattern, you maintain strict linting for production code while granting developers the flexibility to debug tests with console statements.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.