[英]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.