You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何解决Cypress中的‘Cannot find name 'Cy'.’错误?

解决Cypress报错“Cannot find name 'Cy'.”的方案
  • 检查Cypress类型定义配置

    • 若为TypeScript项目,打开tsconfig.json,确认compilerOptions.types数组包含cypress:
      {
        "compilerOptions": {
          "types": ["cypress"]
        }
      }
      
    • 若为JavaScript项目,在cypress/support/index.js(或.ts)开头添加类型引用:
      /// <reference types="cypress" />
      
  • 同步Cypress版本与类型定义

    • 新版Cypress已内置类型定义,无需单独安装@types/cypress,直接更新到最新稳定版:
      npm install cypress@latest --save-dev
      
    • 若之前单独安装过@types/cypress,建议卸载:
      npm uninstall @types/cypress --save-dev
      
  • 验证测试文件配置

    • 确保测试文件使用Cypress识别的后缀:.cy.js、.cy.ts、.spec.js或.spec.ts
    • 检查cypress.config.js(或.ts)中的specPattern配置,确保匹配测试文件路径:
      export default defineConfig({
        e2e: {
          specPattern: 'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}',
        },
      });
      
  • 清理缓存并重启服务

    • 清理Cypress缓存:
      npx cypress cache clear
      
    • 关闭当前Cypress窗口,重新启动:
      npx cypress open
      
  • 重置IDE类型服务

    • 若使用VS Code,按下Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),执行TypeScript: Restart TS Server命令重启类型服务
    • 确认IDE已安装并启用Cypress官方插件,保证类型提示功能正常

内容的提问来源于stack exchange,提问作者Usman Musharaf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 11:00:42