-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: added blog Get Front-End Test Coverage with Cypress
#227
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2eb34d0
docs: added blog `Get Front-End Test Coverage with Cypress`
guoqqqi 63ad1f8
fix: CI error
guoqqqi db9be1b
fix: CI error
guoqqqi 2b11be4
fix: update codes
guoqqqi 953d3ab
fix: update codes
guoqqqi 757b009
fix: update codes
guoqqqi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
website/blog/2021-03-02-get-front-end-test-coverage-with-cypress.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,84 @@ | ||||
--- | ||||
title: "Get Front-End Test Coverage with Cypress" | ||||
author: Yi Sun | ||||
authorURL: "https://github.com/LiteSun" | ||||
authorImageURL: "https://avatars.githubusercontent.com/u/31329157?s=400&u=e81b4bb4db2be162c1fcac6d188f5b0f82f71920&v=4" | ||||
--- | ||||
|
||||
> [@LiteSun](https://github.com/LiteSun), Apache APISIX Committer from [Shenzhen Zhiliu Technology Co.](https://www.apiseven.com/) | ||||
> | ||||
> Source: | ||||
> | ||||
> - https://github.com/apache/apisix | ||||
> - https://github.com/apache/apisix-dashboard | ||||
|
||||
## Background | ||||
|
||||
In the article ["Stable Product Delivery with Cypress"](/blog/2021/02/08/stable-product-delivery-with-cypress), we discussed why we chose Cypress as our E2E testing framework. After spending nearly two months refining the test cases, we needed test coverage to quantify whether the test coverage was sufficient. This article will describe how to use Cypress to get E2E coverage on the front end of APISIX Dashboard. | ||||
|
||||
## What is code coverage? | ||||
|
||||
Code coverage is a metric in software testing that describes the proportion and extent to which the source code in a program is tested, and the resulting proportion is called code coverage. Test code coverage reflects the health of the code to a certain extent. | ||||
|
||||
## Installation Dependencies & Configuration | ||||
|
||||
To collect test coverage data, we need to put some probes in the original business code for Cypress to collect the data. | ||||
|
||||
Cypress officially recommends two approaches, the first is to generate a temporary directory via `nyc` and run the code that has been written to the probe to collect test coverage data. The second way is to do the code conversion in real time through the code conversion pipeline, which eliminates the hassle of temporary folders and makes collecting test coverage data relatively refreshing. We choose the second way to collect front-end E2E coverage. | ||||
|
||||
1. Installing Dependencies | ||||
|
||||
yarn add babel-plugin-istanbul --dev | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
|
||||
2. Install the cypress plug-in | ||||
|
||||
yarn add @cypress/code-coverage --dev | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. |
||||
|
||||
3. Configuring babel | ||||
|
||||
// web/config/config.ts | ||||
extraBabelPlugins: [ | ||||
['babel-plugin-istanbul', { | ||||
"exclude": ["**/.umi", "**/locales"] | ||||
}], | ||||
], | ||||
|
||||
4. Configuring Cypress code coverage plugin | ||||
|
||||
// web/cypress/plugins/index.js | ||||
module.exports = (on, config) => { | ||||
require('@cypress/code-coverage/task')(on, config); | ||||
return config; | ||||
}; | ||||
|
||||
// web/cypress/support/index.js | ||||
import '@cypress/code-coverage/support'; | ||||
|
||||
5. Get Test Coverage | ||||
|
||||
After the configuration is done, we need to run the test case. After the test case is run, Cypress will generate `coverage` and | ||||
`.nyc_output` folders, which contain the test coverage reports. | ||||
|
||||
![1.png](https://lh4.googleusercontent.com/o-tyQagmCjprpNkuTjMFLaALZKtW4pyC9nj-GcPx4MM3xK0zrMED9Nndk5ZmZkZsQ5SIJPEovcrHyjWP2YXtEcYYDpLL49aV_97N83doTkOuMXlFsVjGu53A9FdlxOCr6i3aIDTA) | ||||
|
||||
The test coverage information will appear in the console after executing the following command. | ||||
|
||||
npx nyc report --reporter=text-summary | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. |
||||
|
||||
![2.png](https://lh4.googleusercontent.com/n0CON1WF64wEnh3IYEc3wwwOJ2Ft_WmMLfkhOPKIKxoW0NP6Eq8VplJ87EepL5zIWOeyfJhlDmhc3ImE0ivgRlXWe1RuW2x7vL_JEri7Mz6b3tOY0it8bVvUe83CAHNgeoyXZnsy) | ||||
|
||||
Under the coverage directory, a more detailed report page will be available, as shown here. | ||||
|
||||
![3.png](https://lh4.googleusercontent.com/skjR9YUcbmeytfoYnR0it7Vfc7mheCJDt7PSUsp549IbOdfqskTrIOqUXw01e0fnuNwpGoo3GtqAER3eQjNoTIdmU7HY6hc_sZ5NYc3h-MyxqmVz_NaC3AM-J4rWJFy-9IoTWjpn) | ||||
|
||||
- Statements indicates whether each statement was executed | ||||
|
||||
- Branchs indicates whether each if block was executed | ||||
|
||||
- Functions indicates whether each function is called | ||||
|
||||
- Lines indicates whether each line was executed | ||||
|
||||
## Summary | ||||
|
||||
The test coverage rate reflects the quality of the project to a certain extent. At present, APISIX Dashboard front-end E2E coverage rate has reached 71.57%. We will continue to work with the community to enhance the test coverage rate and provide more reliable and stable products for users. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.