Home > Net >  File is not under 'rootDir' in Cypress tsconfig.json
File is not under 'rootDir' in Cypress tsconfig.json

Time:11-24

I'm trying to configure Cypress in my Project. I bumped into a problem with the typescript configuration for Cypress and I need your help! This is how my project looks like:

fronend/
    - cypress/
        -tsconfig.json
    - src/
    - tsconfig.json
    - package.json
    - cypreess.json

This is my tsconfig.json from Cypress directory:

{
  "extends": "../tsconfig.json",
  "include": ["./**/*.ts"],
  "exclude": [],
  "compilerOptions": {
    "types": ["cypress"],
    "lib": ["es2015", "dom"],
    "isolatedModules": false,
    "composite": true
  }
}

This is my tsconfig.json file from frontend directory:

{
  "compilerOptions": {
    "baseUrl": "src",
    "rootDir": "src",
    "target": "es6",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": ["./src/**/*.ts", "./src/**/*.tsx", "./src/**/*.svg"],
  "exclude": ["node_modules", "scripts"],
  "references": [
    {
      "path": "./cypress/"
    }
  ]
}

The Cypress can work fine without "extends": "../tsconfig.json" but I need it because I would like to export files from src directory in my cypress ts files.

Can you please give me ideas about what is wrong with my configurations? I would prefer to do not to change the main tsconfig.json file because it runs our UI. This is my error:

File '/**/fronend/cypress/support/objects/client.ts' is not under 'rootDir' '/**/frontend/src'. 'rootDir' is expected to contain all source files.
  The file is in the program because:
    Imported via '../../support/client' from file '/**/frontend/cypress/integration/EndToEndTests.spec.ts'
    Matched by include pattern './**/*.ts' in '/**/frontend/cypress/tsconfig.json'ts(6059)
tsconfig.json(3, 15): File is matched by include pattern specified here.

CodePudding user response:

Your issue is coming from the fact that the entirety of the cypress folder is not contained under src. When setting the cypress/tsconfig.json file, you can override the rootDir property set in the parent tsconfig.json file.

Try setting that field to the cypress directory.

...
"compilerOptions: {
    "rootDir": "./"
    ...
},
...

^ The above may work, since the tsconfig is within the cypress directory.

  • Related