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

Cypress support/e2e.js文件无效报错,截图存报告及TS适配疑问

解决Cypress Support文件报错及Typescript适配问题

一、先解决当前报错问题

报错提示support文件缺失或无效,优先排查以下两点:

1. 检查文件路径是否正确

确认你的support/e2e.js文件是否在项目根目录下。如果是Cypress 10+默认目录结构,support文件夹嵌套在cypress目录下,此时配置里的路径应修改为:

supportFile: 'cypress/support/e2e.js',

若为自定义目录结构,务必保证路径是相对于项目根目录的正确路径。

2. 修复文件语法错误

你的e2e.js代码中使用了HTML转义的",这会导致JS语法解析失败,Cypress会判定文件无效。将所有"替换为正常的双引号或单引号:

import addContext from "mochawesome/addContext"

Cypress.on("test:after:run", (test, runnable) => {  
    if (test.state === "failed") {    
        const screenshot = `assets/${Cypress.spec.name}/${runnable.parent.title} -- ${test.title} (failed) (attempt 2).png`;    
        addContext({ test }, screenshot);  
    }
});

3. 额外检查:重启Cypress

如果刚修改了文件扩展名或配置,重启Cypress客户端,确保配置生效。

二、关于Typescript适配问题

测试用例用TS,support文件不强制要求改为TS,JS文件依然可以正常工作。若想统一用TS,按以下步骤修改:

1. 重命名文件

将support/e2e.js重命名为support/e2e.ts,同时同步修改cypress.config.ts里的supportFile路径配置。

2. 完善类型定义

TS版本代码需补充必要类型,避免类型报错:

import addContext from "mochawesome/addContext";

// 为test和runnable添加Cypress官方类型
Cypress.on("test:after:run", (test: Cypress.ObjectLike, runnable: Cypress.ObjectLike) => {  
    if (test.state === "failed") {    
        const screenshot = `assets/${Cypress.spec.name}/${runnable.parent.title} -- ${test.title} (failed) (attempt 2).png`;    
        addContext({ test }, screenshot);  
    }
});

若需要更精确的类型,可安装@types/mochawesome(可选):

npm install @types/mochawesome --save-dev

3. 确认TS配置

确保项目根目录下的tsconfig.json包含Cypress的类型引用:

{
  "compilerOptions": {
    "types": ["cypress", "mochawesome"]
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 01:51:12