简体   繁体   English

Cypress 测试覆盖率(Create-react-app CRA Typescript)

[英]Cypress Test Coverage (Create-react-app CRA Typescript)

I've been trying to set up Cypress code covereage based on their documentation, but it doesnt cover CRA (Create-react-app) in 2022 with Typescript, so i've been having some trouble.我一直在尝试根据他们的文档设置赛普拉斯代码覆盖范围,但它不包括 2022 年使用 Typescript 的 CRA(Create-react-app),所以我遇到了一些麻烦。

My folder structure is: root/cypress/support root/cypress/plugins root/cypress/components root/cypress/e2e我的文件夹结构是:root/cypress/support root/cypress/plugins root/cypress/components root/cypress/e2e

Contents of support/index.js: support/index.js 的内容:

import "@cypress/code-coverage/support";

contents of plugins/index.js plugins/index.js 的内容

    module.exports = (on, config) => {
  require("@cypress/code-coverage/task")(on, config);
  return config;
};

Cypress.config.ts:赛普拉斯.config.ts:

import { defineConfig } from "cypress";
export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {},
  },
  component: {
    supportFile: "cypress/support/index.js",
    supportFolder: "cypress/support",
    fixturesFolder: "cypress/fixtures",
    devServer: {
      framework: "create-react-app",
      bundler: "webpack",
    },
  },
});

Spec file:规格文件:

import * as React from "react";
import App from "../../src/app/App";
import { mount } from "cypress/react";

describe("App", function () {
  it("Should create <App>", () => {
    mount(<App />);
    cy.get("[data-testid=AppContainer]").should("exist");
  });
});

Dev dependencies and scripts:开发依赖项和脚本:

  "devDependencies": {
    "@cypress/code-coverage": "^3.10.0",
    "@cypress/instrument-cra": "^1.4.0",
    "@cypress/webpack-dev-server": "^2.0.0",
    "@svgr/webpack": "^6.2.1",
    "@types/eslint": "^8.4.5",
    "@types/eslint-config-prettier": "^6.11.0",
    "@types/eslint-plugin-prettier": "^3.1.0",
    "@types/prettier": "^2.6.3",
    "@typescript-eslint/eslint-plugin": "^5.30.5",
    "@typescript-eslint/parser": "^5.30.5",
    "cypress": "^10.3.1",
    "eslint": "^8.19.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-prettier": "^4.2.1",
    "gh-pages": "^4.0.0",
    "istanbul-lib-coverage": "^3.2.0",
    "nyc": "^15.1.0",
    "prettier": "^2.7.1",
    "react-scripts": "^5.0.1",
    "start-server-and-test": "^1.14.0",
    "vite": "^2.7.0"
  },
  "scripts": {
    "start": "react-scripts -r @cypress/instrument-cra start",
    "build": "react-scripts build",
    "e2e": "cypress open",
    "cypress:start:app": "SET BROWSER=NONE&& react-scripts -r @cypress/instrument-cra start",
    "cypress:start:wait": "start-server-and-test cypress:start:app http://localhost:3000/mcswf",
    "cypress:open": "npm run cypress:start:wait -- \"cypress open\"",
    "cypress:run": "npm run cypress:start:wait -- \"cypress run\"",
    "eject": "react-scripts eject",
    "predeploy": "npm run build",
    "deploy": "gh-pages -b GitHubPage -d build"
  },

Error crom cypress:错误 cro 柏树:

在此处输入图像描述

log⚠️ Code coverage tasks were not registered by the plugins file. See support issue for possible workarounds. [@cypress/code-coverage]

Please let me know if I'm doing anything incorrectly.如果我做错了什么,请告诉我。 thank you so much太感谢了

Since you are using Cypress v10, the plugin should be set up in cypress.config.js .由于您使用的是 Cypress v10,因此应在cypress.config.js中设置插件。 plugins/index.js is ignored in the latest Cypress version. plugins/index.js在最新的赛普拉斯版本中被忽略。

Also, there's a separate (new) distribution for component code coverage.此外,还有一个单独的(新的)组件代码覆盖分布。 Install it like this:像这样安装它:

npm i -D @bahmutov/cypress-code-coverage

cypress.config.js cypress.config.js

import { defineConfig } from "cypress";
export default defineConfig({
  e2e: {
    ...
  },
  component: {
    setupNodeEvents(on, config) {
      require('@bahmutov/cypress-code-coverage/plugin')(on, config)
      return config
    },
    supportFile: "cypress/support/index.js",
    supportFolder: "cypress/support",
    fixturesFolder: "cypress/fixtures",
    devServer: {
      framework: "create-react-app",
      bundler: "webpack",
    },
  },
});

This and other details here Component Code Coverage in Cypress v10这个和其他细节在这里Component Code Coverage in Cypress v10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Create-react-app + TypeScript + CSS 模块:自动生成类型定义而不从 CRA 中弹出 - Create-react-app + TypeScript + CSS Modules: Auto-generating type definitions without ejecting from CRA 反应由 CRA(create-react-app) 创建的 web 总是重定向到 '/' - react web created by CRA(create-react-app) always redirect to '/' 如何在CRA(create-react-app)中对资产进行版本控制? - How to do versioning for assets in a CRA (create-react-app)? Netlify 使用 create-react-app (CRA) 网站构建错误 - Netlify build Error using create-react-app (CRA) website 如何在没有 create-react-app 的情况下像在 CRA 中一样配置代理? - How to configure a proxy like in CRA without create-react-app? 从 create-react-app (CRA) 转换为 CRA + Next.Js 应用 - Conversion from an create-react-app (CRA) to CRA + Next.Js app 使用 create react app 和 typescript 设置测试覆盖率和阈值 - Setting up test coverage and thresholds with create react app and typescript 使用typescript将sass加载到create-react-app中 - loading sass in a create-react-app with typescript 使用TypeScript在create-react-app中创建故事书 - Storybook in create-react-app with TypeScript create-react-app 可以与 TypeScript 一起使用吗 - Can create-react-app be used with TypeScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM