为什么我得到 "parserOptions.project" has been set for @typescript-eslint/parser specifically for .test.ts files?

Why am I getting "parserOptions.project" has been set for @typescript-eslint/parser specifically for .test.ts files?

过去三天我一直在尝试修复 .test.ts 个文件的这个错误。

我的tsconfig如下

{
  "compilerOptions": {
    "target": "es6",                                     /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    "lib": ["ESNext"],                                   /* Specify a set of bundled library declaration files that describe the target runtime environment. */
   "module": "commonjs",                                /* Specify what module code is generated. */
    "rootDir": "./src",                                  /* Specify the root folder within your source files. */
    "moduleResolution": "node",                          /* Specify how TypeScript looks up a file from a given module specifier. */
    "baseUrl": "src",                                    /* Specify the base directory to resolve non-relative module names. */
    "types": ["jest", "node"],                                      /* Specify type package names to be included without being referenced in a source file. */
    "resolveJsonModule": true,                           /* Enable importing .json files */
    "allowJs": true,                                     /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
    "checkJs": true,                                     /* Enable error reporting in type-checked JavaScript files. */
   "outDir": "./dist",                                  /* Specify an output folder for all emitted files. */
    "removeComments": true,                              /* Disable emitting comments. */
    "importsNotUsedAsValues": "error",                   /* Specify emit/checking behavior for imports that are only used for types */
    "isolatedModules": true,                             /* Ensure that each file can be safely transpiled without relying on other imports. */
    "allowSyntheticDefaultImports": true,                /* Allow 'import x from y' when a module doesn't have a default export. */
    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
    // "preserveSymlinks": true,                         /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
    /* Type Checking */
    "strict": true,                                      /* Enable all strict type-checking options. */
    "noImplicitAny": true,                               /* Enable error reporting for expressions and declarations with an implied `any` type.. */
    "noUnusedLocals": true,                              /* Enable error reporting when a local variables aren't read. */
    "noUnusedParameters": true,                          /* Raise an error when a function parameter isn't read */
    "exactOptionalPropertyTypes": false,                 /* Interpret optional property types as written, rather than adding 'undefined'. */
    "noImplicitReturns": true,                           /* Enable error reporting for codepaths that do not explicitly return in a function. */
    "noFallthroughCasesInSwitch": true,                  /* Enable error reporting for fallthrough cases in switch statements. */
    "noUncheckedIndexedAccess": true,                    /* Include 'undefined' in index signature results */
    "noImplicitOverride": true,                          /* Ensure overriding members in derived classes are marked with an override modifier. */
    "noPropertyAccessFromIndexSignature": true,          /* Enforces using indexed accessors for keys declared using an indexed type */
    "allowUnusedLabels": false,                          /* Disable error reporting for unused labels. */
    "allowUnreachableCode": false,                       /* Disable error reporting for unreachable code. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  },
  "include": ["src"],
  "exclude": ["node_modules", "dist", "coverage", "src/__tests__", "**/*.test.ts"]
}

我的eslintrc.js有这些内容

module.exports = {
  env: {
    es2021: true,
    node: true,
    jest: true,
  },
  extends: [
    'eslint:recommended',
    'airbnb-base',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
    'plugin:prettier/recommended',
    'plugin:import/typescript',
    'prettier'
  ],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    sourceType: 'module',
    project: 'tsconfig.json',
    tsconfigRootDir: './',
  },
  plugins: ['@typescript-eslint', 'prettier', 'import'],
  rules: {
 }
}

src/__tests__/sample.test.ts 我得到这个错误

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

我注意到它仅发生在 .test.ts 文件中,其余仅具有 .ts 扩展名的打字稿文件工作正常

我错过了什么?

因此,由于我在 tsconfig,json 中排除了测试文件,因此我不得不在开发期间以某种方式将这些文件添加到 lint 中,并在构建时排除它们。

我最终创建了一个包含以下内容的新文件 tsconfig.eslint.json

{
  "extends": "./tsconfig.json", <-- since the entire src folder is already included here
  "exclude": ["node_modules", "dist", "coverage"] <--- override the exclude property to not have test files here
}

在我的 eslintrc.js

  parserOptions: {
    sourceType: 'module',
    project: 'tsconfig.eslint.json',
    tsconfigRootDir: './',
  },

这修复了错误。