Azure 管道中的 Cypress 构建错误:找不到模块“@cypress/code-coverage/task”

Cypress build error in Azure pipeline: Cannot find module '@cypress/code-coverage/task'

这是我的配置:

// cypress/plugins/index.js
module.exports = (on, config) => {
  require('@cypress/code-coverage/task')(on, config);
  //require('@bahmutov/cypress-extends')(on, config);
  return config
}

尝试在 Azure 管道脚本(在 cypress/included 容器内)中 运行 cypress 时出现错误。当我在本地 运行 时不会发生此错误。

The function exported by the plugins file threw an error.

We invoked the function exported by `/root/e2e/cypress/plugins/index.js`, but it threw an error.

 Error: Cannot find module '@cypress/code-coverage/task'
Require stack:
- /root/e2e/cypress/plugins/index.js
- /root/.cache/Cypress/9.1.1/Cypress/resources/app/packages/server/lib/plugins/child/run_plugins.js

我唯一不寻常的事情是:

// cypress/config/cypress.local.json
{
  "extends": "../../cypress.json",
  "baseUrl": "https://localhost:4200"
}

和正常的 cypress.json 配置:

// /cypress.json
{
  "baseUrl": "http://localhost:4200",
  "proxyUrl": "",
  "defaultCommandTimeout": 10000,
  "video" : false,
  "screenshotOnRunFailure" : true,
  "experimentalStudio": true,
  "projectId": "seixri",
  "trashAssetsBeforeRuns" : true,
  "videoUploadOnPasses" : false,
  "retries": {
    "runMode": 0,
    "openMode": 0
  },
  "viewportWidth": 1000,
  "viewportHeight": 1200
}

这里的问题可能是 Cypress 不支持以您的方式扩展配置文件,正如此处所述:https://www.cypress.io/blog/2020/06/18/extending-the-cypress-config-file/

在我看来有两种合适的解决方法:

1.方法:使用单独的配置文件(我的建议)

由于扩展现有配置文件不起作用,我建议使用单独的配置文件,例如一种用于本地使用,另一种用于在 Azure 管道中执行。然后,您可以在 package.json 中简单地添加两个单独的命令,例如:

  "scripts": {
    "cy:ci": "cypress run --config-file cypress/cypress.json",
    "cy:local": "cypress run --config-file cypress/cypress.local.json"
  },

文档:https://docs.cypress.io/guides/references/configuration

2。方法:在测试中设置配置选项

Cypress 为您提供了直接在测试中覆盖配置的选项。例如,如果您在 cypress.json 中配置了以下内容:

{
  "viewportWidth": 1280,
  "viewportHeight": 720
}

您可以在测试中更改 viewportWidth,例如:

Cypress.config('viewportWidth', 800)

文档:https://docs.cypress.io/api/cypress-api/config#Syntax