【问题标题】:Getting code coverage reports with Cypress and React app使用 Cypress 和 React 应用程序获取代码覆盖率报告
【发布时间】:2021-12-01 17:47:54
【问题描述】:

我正在处理一个客户给我的项目,该项目具有现有的代码库。它基于 React,不使用 Create-React-App。我根本不是最精通使用 babel 和 webpack,因为我通常依赖 CRA 和 react-scripts。我正在使用 Cypress 和 Istanbul 进行单元测试,在遵循文档和几个指南之后,我无法生成代码覆盖率报告。

我不完全确定任何寻求帮助的人都需要哪些代码,所以我会将我认为相关的内容放在这里,并根据任何评论者的建议添加。

我的 babelrc 文件:

  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": "current"
        }
      },
      "@babel/preset-react"
    ]
  ],
  "plugins": [
    [
      "module-resolver",
      {
        "root": [
          "./src"
        ]
      }
    ],
    [
      "@babel/plugin-syntax-dynamic-import"
    ],
    [
      "transform-class-properties"
    ],
    [
      "istanbul"
    ]
  ],
  "env": {
    "test": {
      "plugins": [
        "istanbul"
      ]
    },
    "e2e": {
      "plugins": [
        "istanbul"
      ]
    }
  }
}

柏树插件的index.js:

// ***********************************************************
// This example plugins/index.js can be used to load plugins
//
// You can change the location of this file or turn off loading
// the plugins file with the 'pluginsFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/plugins-guide
// ***********************************************************

// This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)

/**
 * @type {Cypress.PluginConfig}
 */
// eslint-disable-next-line no-unused-vars
module.exports = (on, config) => {
  require('@cypress/code-coverage/task')(on, config)
  on('file:preprocessor', require('@cypress/code-coverage/use-babelrc'))
  return config
}

我对 Cypress 的支持文件夹的 index.js:

import '@cypress/code-coverage/support'

以及我为测试而执行的脚本

 "cy": "cypress run && nyc report --reporter=text --reporter=text-summary"

一切都由纽约市检测。

感谢您的帮助!

【问题讨论】:

  • 最后我发现我没有以某种方式安装 NYC 作为依赖项。除此之外,我的 package.json 不包含以下行:` "nyc": { "all": true, "include": "src/*.js" }` 添加后,它运行良好。

标签: reactjs cypress


【解决方案1】:

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
猜你喜欢
  • 1970-01-01
  • 2020-12-21
  • 2014-09-09
  • 2018-08-09
  • 2021-07-23
  • 2021-08-19
  • 2019-12-14
  • 1970-01-01
  • 2020-01-12
相关资源
最近更新 更多