拆分setupNodeEvents后cypress-mochawesome-reporter无法生成HTML报告
问题描述
我使用版本为^3.3.0的cypress-mochawesome-reporter插件,在将setupNodeEvents拆分为独立函数前能正常生成HTML测试报告;拆分后仅生成JSON文件,无法生成HTML报告。
我的cypress.config.js配置代码
const { defineConfig } = require("cypress"); const preprocessor = require("@badeball/cypress-cucumber-preprocessor"); const browserify = require("@badeball/cypress-cucumber-preprocessor/browserify"); async function setupNodeEvents(on, config) { screenshotOnRunFailure = true; require("cypress-mochawesome-reporter/plugin")(on); // for html reports // This is required for the preprocessor to be able to generate JSON reports after each run, and more, await preprocessor.addCucumberPreprocessorPlugin(on, config); on("file:preprocessor", browserify.default(config)); // Make sure to return the config object as it might have been modified by the plugin. return config; } module.exports = defineConfig({ reporter: "cypress-mochawesome-reporter", // for html reports env: { baseUrl: "https://rahulshettyacademy.com/angularpractice/", }, e2e: { setupNodeEvents, specPattern: "cypress/integration/Tests/*.js", // specPattern: "cypress/integration/Tests/BDD/*.feature", downloadsFolder: "cypress/downloads", }, });
生成的文件情况

解决方案
拆分setupNodeEvents为独立函数后,异步执行顺序影响了cypress-mochawesome-reporter的初始化,加上cucumber预处理器的操作可能覆盖了reporter配置,按以下步骤修改:
- 调整插件初始化顺序:确保
cypress-mochawesome-reporter的插件注册在cucumber预处理器之前完成 - 显式保留reporter配置:在返回config前,重新指定reporter,避免被其他插件修改
- 检查依赖包:确认安装了生成HTML所需的依赖
修改后的setupNodeEvents函数:
async function setupNodeEvents(on, config) { screenshotOnRunFailure = true; // 先初始化mochawesome报告插件 require("cypress-mochawesome-reporter/plugin")(on); // 处理cucumber预处理器的异步配置 await preprocessor.addCucumberPreprocessorPlugin(on, config); on("file:preprocessor", browserify.default(config)); // 显式设置reporter,防止被覆盖 config.reporter = "cypress-mochawesome-reporter"; return config; }
安装缺失的依赖(如果未安装):
npm install mochawesome-merge mochawesome-report-generator --save-dev
另外,运行测试时不要在命令行添加--reporter参数,避免覆盖配置文件中的设置。
内容的提问来源于stack exchange,提问作者Neel Chavan
相关产品推荐
相关产品推荐

