64

I am setting up a new project with Remix (remix.run) and I am trying to configure Cypress/ESLint for component testing. I have a TestComponent.cy.ts with some boilerplate code:

describe('TestComponent.cy.ts', () => {
  it('playground', () => {
    // cy.mount()
  })
})

However, the describe function throws this Error:

    Parsing error: ESLint was configured to run on `<tsconfigRootDir>/component/TestComponent.cy.ts` using `parserOptions.project`: <tsconfigRootDir>/../../../../../../users/tduke/desktop/dev/blog/cypress/tsconfig.json
    However, that TSConfig does not include this file. Either:
    - Change ESLint's list of included files to not include this file
    - Change that TSConfig to include this file
    - Create a new TSConfig that includes this file and include it in your parserOptions.project

I have tried to reconfigure my .tsconfig.json and .eslintrc.js to no avail. Currently, this is what those files look like:

tsconfig.json:

{
  "exclude": ["./cypress", "./cypress.config.ts"],
  "include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx", "./cypress/component/*.cy.ts", "./cypress/**/*.cy.ts", 
  ],
  "compilerOptions": {
    "lib": ["DOM", "DOM.Iterable", "ES2019"],
    "types": ["vitest/globals"],
    "isolatedModules": true,
    "esModuleInterop": true,
    "jsx": "react-jsx",
    "module": "CommonJS",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "target": "ES2019",
    "strict": true,
    "allowJs": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "paths": {
      "~/*": ["./app/*"]
    },
    "skipLibCheck": true,

    // Remix takes care of building everything in `remix build`.
    "noEmit": true
  }
}

.eslintrc.js:


/** @type {import('@types/eslint').Linter.BaseConfig} */
module.exports = {
  extends: [
    "@remix-run/eslint-config",
    "@remix-run/eslint-config/node",
    "@remix-run/eslint-config/jest-testing-library",
    "prettier",
  ],
  env: {
    "cypress/globals": true,
  },
  parserOptions: {
    project: './tsconfig.json'
  },
  plugins: ["cypress"],
  // We're using vitest which has a very similar API to jest
  // (so the linting plugins work nicely), but we have to
  // set the jest version explicitly.
  settings: {
    jest: {
      version: 28,
    },
  },
};

CC BY-SA 4.0
1
  • If you encounter this in Next.js/Turbo workspace, potential solution is to exclude out directory generated by previously running pnpm build from eslint configuration. To do that add ignorePatterns to your .eslintrc.js. Commented Mar 30 at 23:16

17 Answers 17

52

I also encountered the same problem, I solved the problem by removing the project property under parserOptions

parserOptions: {
  ecmaFeatures: {
    jsx: true,
  },
  ecmaVersion: 12,
  sourceType: 'module',
  // project: 'tsconfig.json',
  tsconfigRootDir: __dirname,
},
CC BY-SA 4.0
5
40

This is what I ended up putting in my .eslintrc.js:

module.exports = {
  // ...
  parserOptions: {
    parser: '@typescript-eslint/parser',
    project: './tsconfig.json',
    tsconfigRootDir: __dirname,
  },
};

It may have been that the project needed to be ./tsconfig.json instead of tsconfig.json. My tsconfig file is in the project root.

And this brought up some more errors linting things like babel config so I created an .eslintignore file and added those in there:

.eslintrc.js
ios/
android/
*.config.js

This solved all my issues.

CC BY-SA 4.0
2
  • 6
    This is probably going to be the solution in most cases. The error literally explains that ES Lint is running a TypeScript check on a set of files with help of your TypeScript configurations in TSConfig (tsconfig.json), but the TSConfig provided excludes those files. The natural solution is to also exclude these files from ES Lint, with help of .eslintignore.
    – JacobF
    Commented Jan 30, 2023 at 16:04
  • 2
    This is the best answer. Don't forget tsconfigRootDir
    – Hinton
    Commented May 19, 2023 at 9:00
22

According to your tsconfig include you have included the .cy.ts files from your /cypress folder. But the error is about the file in your root /component folder.

By the way you're also missing the root dir config. Try setting the root dir in .eslintrc.js:

parserOptions: {
    project: './tsconfig.json',
    tsconfigRootDir: __dirname,
  },

Then include or exclude the cypress files (depending on your needs) in tsconfig.json:

...
"**/*.cy.ts"
...

Also you're not excluding the node_modules folder. I don't know whether it's intentional or not. But when you define exclude, it overrides the default, and you should exclude node_modules manually.

If your tsconfig doesn't include some files from the root directory (e.g. .eslintrc.js) you have to exclude them in .eslintrc.js itself:

   ignorePatterns: ['.eslintrc.js'],

This way you don't have to include development only files in your tsconfig.

It's the same as .eslintignore file Ian suggested above. But for those who don't like creating files with literally one line of content in them!

Hope it helps you or someone else in the future.

For more info:

https://typescript-eslint.io/linting/troubleshooting#i-get-errors-telling-me-eslint-was-configured-to-run--however-that-tsconfig-does-not--none-of-those-tsconfigs-include-this-file

CC BY-SA 4.0
16

You should add ["./.eslintrc.js"] to your tsconfig.json file:

{
   "include": ["./.eslintrc.js"],
   //...
}
CC BY-SA 4.0
2
6

After trying all the answer it was adding "tsconfigRootDir" without changing anything else that worked for me.

parserOptions: {
  project: true,
  tsconfigRootDir: __dirname, // added
},
CC BY-SA 4.0
1
3

I am using Vite, i had nearly the same issue and I solved it by changing include in tsconfig file:

"include": [
    "vite.config.ts",
    ".eslintrc.cjs",
    "src"
  ], 
CC BY-SA 4.0
1
2

It will happening because Eslint self trying to parse .eslintrc.cjs file

so simply we need to do go to your .eslintrc.cjs file and then add ignorePatterns like below

module.exports = {
    ...

    extends: [
        'eslint:recommended',
        'plugin:@typescript-eslint/recommended-type-checked'
    ],
    
    ignorePatterns: ["dist", ".eslintrc.cjs"],
    ...

}
CC BY-SA 4.0
2
1

i have same issue and was fixed done. It get error message with encountering suggests that ESLint's TypeScript parser is not considering the file api.ts located in packages/cdk/lib/construct/ as part of the project specified in tsconfig.eslint.json. This can occur when the include pattern in tsconfig.eslint.json does not match the file's location.

To address this, ensure that the tsconfig.eslint.json(create if not existed) includes all the directories and files you intend to lint.

Try updating tsconfig.eslint.json as follows:

{
  "include": [
    "src/**/*.ts",
    "lambda/**/*.ts",
    "lib/**/**/*.ts", // Adjusted pattern to include subdirectories
    "*.ts",
    ".eslintrc.cjs"
  ],
  "compilerOptions": {
    "noEmit": true
  }
}

Below is sample eslintrc.cjs (my path is : packages\cdk.eslintrc.cjs)

module.exports = {
  root: true,
  globals: {
    __dirname: 'readonly',
  },
  env: {
    node: true,
  },
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
  ],
  ignorePatterns: ['cdk.out'],
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: './tsconfig.eslint.json',
    tsconfigRootDir: __dirname,
  },
  plugins: ['@typescript-eslint'],
  rules: {
    '@typescript-eslint/no-namespace': 'off',
  },
};
CC BY-SA 4.0
1

.eslintrc.js:

module.exports = {
  root: true // <--- added this
  // ...
};
CC BY-SA 4.0
0

This happened due you may be missing to include your .eslintrc.js file in tsconfig.json include list. You include that and error will be resolved

CC BY-SA 4.0
0

So @Ian answer was half right for me.

Basically tsconfig is ignoring jest.config.ts but eslint is including it.

Simple solution is to add to or create .eslintignore

# jest
jest.config.*

This just tells eslint to also ignore this file which is what you want.

CC BY-SA 4.0
0

I had the same problem so I used overrides in my .eslintrc file after creating a separate tsconfig.test.json and it went well.

Here is tsconfig.test.json:

{
  "extends": "./tsconfig.json",
  "include": ["**/__tests__/**/*.test.ts"]
}

and here is .eslintrc:

 {
      "root": true,
      "env": {
        "browser": true,
        "es2021": true
      },
      "plugins": ["@typescript-eslint", "prettier"],
      "extends": ["airbnb", "airbnb-typescript", "airbnb/hooks", "prettier"],
      "parser": "@typescript-eslint/parser",
      "parserOptions": {
        "ecmaFeatures": {
          "jsx": true
        },
        "ecmaVersion": "latest",
        "sourceType": "module",
        "project": "./tsconfig.json",
        "warnOnUnsupportedTypeScriptVersion": false
      },
      "settings": {
       // some settings
      },
      "rules": {
        // some rules
      },
      "overrides": [
        {
          "files": ["src/**/*.ts"],
          "parserOptions": {
            "project": "./tsconfig.json"
          }
        },
        {
          "files": ["__tests__/**/*.test.ts"],
          "parserOptions": {
            "project": "./tsconfig.test.json"
          }
        }
      ]
    }

So basically what is happening here is that your ESLint configuration will now include the appropriate overrides for using different TypeScript configurations for your main project files as well as test files.

CC BY-SA 4.0
1
  • But when trying to run the test by "npx jest" I have got this error ``` FAIL __tests__/utils/flattenArray.test.ts ● Test suite failed to run Jest encountered an unexpected token Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax. Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration. By default "node_modules" folder is ignored by transformers.```
    – lighthouse
    Commented Aug 21, 2023 at 10:35
0

I have a react.js project created with create-react-app. I have an src folder with my react project and cypress folder with cypress tests. I fixed this problem by simply adding cypress folder into my tsconfig.json file:

{
  "compilerOptions": {
    // ... some standard config stuff for react
  },
  "include": [
    "src",
    "cypress" // <--- added this and now my eslint also works for `cypress` folder
  ]
}
CC BY-SA 4.0
0

I got this error for vite.config.ts in my Vite TypeScript project, and I fixed it by adding vite.config.ts to the ignorePatterns in my .eslintrc.cjs:

module.exports = {    
  ignorePatterns: [
    'vite.config.ts', // Add this line
  ],
};
CC BY-SA 4.0
0

Fixed this error in a Next.js project by changing include in tsconfig.json:

  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]

to

  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"]
CC BY-SA 4.0
0

Including directories of built files or generated to ignorePatterns solved for me.

ignorePatterns: [
  "/build/**/*", // Ignore built files.
  "/generated/**/*", // Ignore generated files.
],
CC BY-SA 4.0
0

In my case this happens when i added a tailwind.config.ts file in a shared package using turbo repo. And the fix was to add "tailwind.config.ts" to the tsconfig.lint.json file.

 {
  "extends": "@repo/typescript-config/react-library.json",
  "compilerOptions": {
    "outDir": "dist"
  },
  "include": ["src", "turbo", "tailwind.config.ts"],
  "exclude": ["node_modules", "dist", "./tailwind.config.js"]
}
CC BY-SA 4.0

Not the answer you're looking for? Browse other questions tagged or ask your own question.