skip to content

In ESLint 10, what does `eslint.config.js` export, and how do you apply a rule only to test files?

level: juniorimportance: must knowfreq 40%

answer

  1. an ordered list, not one object
  2. defineConfig() from eslint/config
  3. a files key scopes an object
  4. the later matching object wins
  5. defaults: **/*.js, **/*.cjs, **/*.mjs

basics

~20 s

An eslint.config.js file exports an array of configuration objects, usually wrapped in defineConfig(). To target tests, append an object whose files key holds a test glob such as **/*.test.js and whose rules apply only to those files.

solid answer

~40 s

In ESLint 10 flat config is the only format: an `eslint.config.js` (or `.mjs`, `.cjs`, or a `.ts` / `.mts` / `.cts` file with extra setup) at the project root exports an array of configuration objects, usually through `defineConfig()` from `eslint/config`. Each object may carry `files`, `ignores`, `languageOptions`, `rules` and a few other keys. An object without `files` applies to every file that some other object matches, which by default means `**/*.js`, `**/*.cjs` and `**/*.mjs`. For tests, append an object with `files: ["**/*.test.js"]` and the test-only `rules` or `languageOptions.globals`. Because later matching objects override earlier ones, it goes after the base objects. `.eslintrc.*` files are not read at all since 10.0.0.

code

javascript · 16 lines
javascript
// eslint.config.js
import js from "@eslint/js";
import { defineConfig } from "eslint/config";

export default defineConfig([
  {
    files: ["**/*.js"],
    extends: [js.configs.recommended],
    rules: { "no-console": "error" },
  },
  {
    files: ["**/*.test.js"],
    languageOptions: { globals: { describe: "readonly", it: "readonly" } },
    rules: { "no-console": "off" },
  },
]);

go deeper

for a junior

Recall that the file exports an array of objects and that a later object with a files glob is how test files get their own rules.

for a middle

Explain how an object without files inherits the matched file set, why array order decides conflicts, and which extensions ESLint lints by default.

for a senior

Show you can predict the merged result for a given file, and that you know which file wins when a directory holds several eslint.config variants or a CommonJS package.

for a principal

Talk about keeping one config array readable as it grows: named objects, base settings first, narrow exceptions last, and no reliance on glob specificity.

## What `eslint.config.js` is In ESLint 10 there is exactly one configuration format, called **flat config**. The configuration is a JavaScript (or TypeScript) module named `eslint.config.*`, normally at the project root, and its default export is a **config array**: an ordered array of plain **configuration objects**. ESLint 10 no longer reads `.eslintrc.json`, `.eslintrc.js` or the `eslintConfig` key in `package.json`. Version 10.0.0 removed that older format, together with the `ESLINT_USE_FLAT_CONFIG=false` switch that ESLint 9 still honoured. Most configs wrap the array in `defineConfig()`, imported from `eslint/config` (exported there since ESLint 9.22.0). The helper gives editors type information and adds support for the `extends` key. Its result is still an ordinary array. ```js // eslint.config.js import js from "@eslint/js"; import { defineConfig } from "eslint/config"; export default defineConfig([ { files: ["**/*.js"], extends: [js.configs.recommended], rules: { "no-console": "error" }, }, { files: ["**/*.test.js"], languageOptions: { globals: { describe: "readonly", it: "readonly" } }, rules: { "no-console": "off" }, }, ]); ``` ## What a configuration object can hold | Key | What it does | |---|---| | `name` | A label shown in error messages and in the config inspector | | `files` | Glob patterns the object applies to | | `ignores` | Globs removed from this object, or from every object when used alone | | `basePath` | A subdirectory the object is scoped to | | `extends` | Configs to inherit (resolved by `defineConfig()`) | | `languageOptions` | `ecmaVersion`, `sourceType`, `globals`, the parser | | `linterOptions` | How inline comments and unused directives are treated | | `rules` | Rule settings for the files the object matches | | `plugins`, `settings`, `processor`, `language` | Extensions and shared data | ## Scoping a rule to test files Three facts decide what a test file receives: - An object **without** `files` applies to every file that any other object matches. It is the place for project-wide settings. - An object **with** `files` applies only to paths its patterns match. Patterns use minimatch syntax and are evaluated relative to the directory that holds the config file. - When several objects match one file, **all of them apply**, merged in array order, and the **later** object wins any conflict. That is why the test object above sits after the base object: for `map.test.js` the second object switches `no-console` off, while `map.js` keeps the error. Glob specificity plays no part. A narrow pattern placed early loses to a broad pattern placed later. ## Settings beyond rules A test object rarely changes only rules. Test files usually rely on globals that a test runner injects, such as `describe` and `it`, and those belong in the same object under `languageOptions.globals`: - Values are `"readonly"` or `"writable"`; `readonly` is the usual choice for injected functions. - Globals from several matching objects **merge**, so the test files keep any project-wide globals and gain the test ones. - `linterOptions` and `languageOptions` can be scoped the same way, for example a different `sourceType` for one folder of CommonJS scripts. Keeping every test-only setting in one named object makes the exception easy to find and easy to delete. ## Which files are linted at all By default ESLint lints files matching `**/*.js`, `**/*.cjs` and `**/*.mjs`. Any other extension, such as `.jsx` or `.ts`, is linted only when some object names it in `files` with a pattern like `"**/*.jsx"` (a pattern that is `*` or ends in `/*` or `/**` does not count). `**/node_modules/` and `.git/` are ignored by default. ESLint also turns no rules on by itself: a config that enables nothing and extends nothing reports nothing. ## File names, formats and precedence When one directory holds several config files, ESLint takes the first in this order: 1. `eslint.config.js` 2. `eslint.config.mjs` 3. `eslint.config.cjs` 4. `eslint.config.ts` 5. `eslint.config.mts` 6. `eslint.config.cts` `--config` (or `-c`) picks a different file explicitly. If `package.json` declares `"type": "commonjs"`, `eslint.config.js` must use `require()` and `module.exports`; naming the file `eslint.config.mjs` keeps ES module syntax instead. A TypeScript config on Node.js needs the optional dev dependency `jiti` at 2.2.0 or later (ESLint 10 dropped older versions), unless you opt into an experimental native loader behind a feature flag. ESLint does not type-check that file and applies no `tsconfig.json` settings to it. ## What interviewers listen for - The answer says **array**, and knows that order inside it matters. - Test settings live in a **separate object with `files`**, not in an `overrides` block, which was eslintrc syntax. - The candidate knows `.eslintrc.json` is ignored in ESLint 10 rather than merged with the new file. - The candidate knows that an extension outside the defaults needs a `files` pattern before ESLint lints it.

  • A folder contains both `eslint.config.js` and `eslint.config.ts`. Which one does ESLint 10 load?
    `eslint.config.js`. ESLint prefers JavaScript over TypeScript config files, in the order `.js`, `.mjs`, `.cjs`, `.ts`, `.mts`, `.cts`, and uses the first one it finds. To load the TypeScript file instead, pass it explicitly with `--config eslint.config.ts`, or delete the JavaScript one.
  • Why might a team's `eslint.config.js` have to use `require()` and `module.exports`?
    Because the nearest `package.json` declares `"type": "commonjs"`, so Node.js loads `.js` files as CommonJS. Either write the config in CommonJS form, or rename it `eslint.config.mjs` to keep `import` and `export default`.
  • A new `eslint.config.js` exports `defineConfig([])` and the run reports nothing. Is ESLint broken?
    No. ESLint enables no rules by itself: something must turn rules on, either a `rules` object or an `extends` entry such as `js.configs.recommended` from `@eslint/js`. The files are still parsed, so a syntax error would still be reported.

  • Puts the test-file rules in an overrides block inside eslint.config.js
  • Believes the most specific files glob wins whatever its position in the array
  • Expects ESLint 10 to fall back to .eslintrc.json when no eslint.config.js exists
  • Thinks an object without files applies only to files no other object matches
  • Expects eslint.config.ts to load on Node.js with no extra dependency

Assumes ESLint 10: flat config is the only format since 10.0.0 removed eslintrc, and defineConfig() is exported from eslint/config since 9.22.0.