skip to Main Content

I have a ReactJS Typescript project with NPM and VSCode.

I am getting the following syntax error in VSCode, even though all my Cypress tests run correctly.

Error: Cannot find name 'cy' 

How can I fix?

enter image description here

I tried the following solution and still receiving syntax errors,

npm install eslint-plugin-cypress --save-dev

"eslintConfig": {
  "extends": [
    "plugin:cypress/recommended"
  ]
},

3

Answers


  1. Chosen as BEST ANSWER

    For Typescript configuration, Add this to tsconfig.json inside your cypress root folder:

    {
      "compilerOptions": {
        "target": "es5",
        "lib": ["es5", "dom"],
        "types": ["cypress", "node"]
      },
      "include": ["**/*.ts"]
    }
    

  2. If using // @ts-check on a Javascript project, you can specify type libraries in jsconfig.json

    {
      "compilerOptions": {
        "lib": ["es2015", "dom"],
        "types": [
          "cypress", 
        ]
      }
    }
    

    Ref: JS Projects Utilizing TypeScript

    Login or Signup to reply.
  3. You can use a triple-slash directive in individual files (only valid at the top of the spec file)

    /// <reference types="cypress" />
    

    This is used to declare a dependency on a package, in this case the Cypress package.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search