9

I'm using the awesome typescript-eslint together with ESLint.

Problem description: the TypeScript ESLint parser complains about src/module.spec.ts not being part of the project, and this is correct. I'm excluding all spec.ts files from TypeScript tsconfig.json file because they don't need to be transpiled.

How to make src/module.spec.ts not transplied but still checked against ESLint?

my-project\src\module.spec.ts 0:0 error Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser. The file does not match your project config: src\module.spec.ts. The file must be included in at least one of the projects provided

My .eslint.json (stripped down):

{
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended"
  ],
  "env": {
    "node": true
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "project": "tsconfig.json"
  },
  "plugins": [
    "@typescript-eslint"
  ]
}

My tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "declaration": true,
    "sourceMap": false,
    "outDir": "./dist",
    "rootDir": "./src",
    "removeComments": true,
    "strict": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "./dist",
    "**/*.spec.ts"
  ]
}
gremo
  • 47,186
  • 75
  • 257
  • 421
  • Not an exact duplicate, but basically the same solution: https://stackoverflow.com/questions/35470511/setting-up-tsconfig-with-spec-test-folder – GOTO 0 Jul 21 '21 at 14:15

1 Answers1

4

In the project folder containing tsconfig.json, create another JSON file with the following contents:

{
  "exclude": [
    "./dist"
  ],
  "extends": "./tsconfig.json"
}

The exact file name is irrelevant here. Note that this is a perfectly valid TypeScript configuration file where all settings are inherited from ./tsconfig.json, execept for extends, where the pattern "**/*.spec.ts" has been removed.

A tool that reads its settings from this new JSON file (as opposed to the default tsconfig.json) will not ignore src/module.spec.ts.

Then in your .eslint.json under parserOptions set the name of the new JSON file as project, e.g. if the file is called test.tsconfig.json:

"project": "test.tsconfig.json"

Then run ESLint and see what else it's complaining about.

This is basically what I'm doing in one of my projects having encountered a similar problem. There must be probably a few other ways to achieve the same effect.

GOTO 0
  • 42,323
  • 22
  • 125
  • 158
  • But test.tsconfig.json will then ovverride the project tsconfig.json right? Or we can have both? – gremo Jun 02 '20 at 18:13
  • Exactly. TypeScript uses `tsconfig.json` per default, while typescript-eslint uses whatever file is set as `project` is the ESLint config. – GOTO 0 Jun 02 '20 at 18:55
  • Oh, I see. Clever. By the way, why repeating exclude key? Isn’t better to name it like tsconfig.eslint.conf instead? Is there any naming convention? – gremo Jun 02 '20 at 21:48
  • See my updated answer. I couldn't find anything about naming conventions, VSCode and other IDEs only recognize `tsconfig.json` as a TS config file. Anyway the [docs](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) provides two examples of alternative names: `configs/base.json` and `tsconfig.nostrictnull.json` (both unconventional, though). – GOTO 0 Jun 03 '20 at 06:46
  • 1
    So in a monorepo, I have to create 2 tsconfigs per project just so I can have my tests linted? – Robula Mar 22 '21 at 16:33