Lesson 8 / 27
Targeting Files, Globals & Overrides
Target files with the files key, set globals and apply different rules to tests in flat config.
Environments and globals
In flat config, languageOptions.globals replaces env, and extra config objects with a files pattern replace overrides. Later objects win for the files they match.
Using overrides
Apply stricter rules to source files but looser rules to tests.
import globals from 'globals';
export default [
{ languageOptions: { globals: globals.browser }, rules: { 'no-console': 'error' } },
{
files: ['**/*.test.js'],
languageOptions: { globals: globals.jest },
rules: { 'no-console': 'off' },
},
{ ignores: ['dist/', 'coverage/'] },
];
Output:
Overrides configured: stricter prod, lenient tests
Selective strictness
Test files and scripts often need looser rules. Add a config object with a files pattern instead of duplicating your base config, and use a top-level ignores object for build output.
Quick check
Quick check: How do you apply different rules to test files in flat config?
- Add a config object with a files pattern
- Use the env key
- Rename the files
- Use .eslintignore only
Answer
Add a config object with a files pattern — Later matching objects override earlier ones.