Unit test is an important part of the front-end knowledge system, and it is the basic knowledge that the middle and advanced front-end must master. However, in the pursuit of development efficiency in the daily work, unit tests are often not paid attention to and applied, nor is it required to write unit tests. As a result, many developers don’t even know how to write unit tests. Not to mention figuring out what their responsibilities are among the many test-related NPM packages?

If you haven’t written unit tests, and you haven’t written unit tests at all, believe me, you’re missing out on a great tool for improving your code and your development confidence.

Anyways, even if you’ve never written a test, you’ve probably seen the Tests directory and test script scripts in Anyways when initializers for front-end projects, or in some open-source github project.

"scripts": {
  "start": "react-scripts start"."build": "react-scripts build"."test": "react-scripts test"."eject": "react-scripts eject"
}
Copy the code

^ Create the project created by the React App

It doesn’t matter if you haven’t written about it before. This series of articles is written for those of you who have never written about testing at all. It will take you step by step through unit testing from scratch.

After reading the first article as an introduction, you will know what unit tests are; What are the characteristics of unit testing, its benefits and limitations? What types of tests are available besides unit tests? Let ‘s go.

What are unit tests?

In computer programming, Unit Testing (English: Unit Testing), also known as module Testing, is a test for the correctness of the program module (the smallest Unit of software design). A program unit is the smallest testable part of an application. In procedural programming, a unit is a single program, function, procedure, etc. For object-oriented programming, the smallest unit is a method, including methods in a base class (superclass), an abstract class, or a derived class (subclass). — Wikipedia

Despite the paragraph above, there are actually two phrases that explain unit testing:

  1. Unit: A single program, function, or procedure is well understood. A pure method, class, or component is a unit.
  2. Testing: Correctness verification is the verification of the code being tested to determine whether the results are as expected.

In addition to unit testing, there is…

In addition to unit testing, there are other types of testing that developers often use

  • Integration test: used to verify the data linkage and communication of multiple units assembled together, as opposed to a single program, function, or procedure.
  • E2E testing: Also known as end-to-end testing, used to verify user operations across multiple application modules.

A pyramid model of types can be seen in many articles on unit testing:

To visualize the differences between the three types of tests, take forms for example. Tests on or
components are unit tests; Multiple controls are combined together to Form a Form, and the Form Form is tested to verify that the normal linkage of each control belongs to the integration test; Using a browser to simulate the operation of a real user. If the data can be written to the back end after the login button is clicked and the data can be jumped to the back end successfully, it belongs to the E2E test.

Combination of the above three types of test pyramid, it can be seen that the pyramid up, the more dependencies, also increases as the unstable factor, test range is becoming more and more extensive, writing unit tests for component design can be all kinds of input, and end-to-end testing phase is limited by other dependence, build test inputs will be very difficult. From the perspective of bug fixing, the cost of fixing bugs in the end-to-end testing phase is higher than fixing bugs in the unit testing phase. Therefore, relatively speaking, unit testing is a cost-effective means of testing. It is generally recommended that the percentages of various types of testing be: unit testing 70%, integration testing 20%, and E2E testing 10%.

Amway: The benefits of unit testing

The list of benefits of writing unit tests is endless, such as:

  • It can help you catch problems early in your software development and fix them before they snowball.
  • Unit testing prevents a bug from recurring and seals it.
  • Combined with automated tools, manual testing can be avoided and the cost of testing after refactoring can be significantly reduced.

.

The most important thing for me is that writing unit tests gives me more confidence in delivering code.

The anti-Amway: Limitations of unit testing

From the comparison of the three test types at 👆, we can see that unit testing has its limitations. Due to “shortsighted”, only focus on the local, can not guarantee the final performance. Take 👇 for example:

Verified that the umbrella can be opened normally, but the goose real effect let head big.

expect(umbrellaOpens).toBe(true)

tests: 1 passed, total
**all tests passed**
Copy the code

conclusion

This article, the first in the Don’t Say You Don’t Know Unit Testing series, covers unit testing, integration testing, end-to-end testing, and the benefits and limitations of unit testing.

The next article will cover the various terms used in unit testing: assertion, test surrogate, test coverage, execution environment, testing framework, and so on. Like + follow is welcome, this will give me more motivation to write technical blog.