April 20, 2022
The importance of unit testing in your project
First, it’s important to remember why unit testing is so important in software projects. As you probably already know, unit testing is essential for testing the individual components of your software project to make sure that each piece is performing as expected.
Unit tests can help you identify where issues are occurring at the component level and help you focus your time and efforts there. It’s necessary to make sure these tests are solid before moving on to integration or smoke testing to help rule out component-level issues before looking at the system as a whole.
Why is automated testing so crucial for unit testing?
When it comes to unit testing, manual tests are simply far too costly to implement and run on a frequent basis across most software projects with significant test coverage.
With automated testing, you can run unit tests across the entire software project upon every build where the code may have changed, making it far less costly to update and run tests on the related code areas.
Since each time you change code, there is a risk of introducing new issues, trying to accomplish the same level of coverage with manual testing would mean you would need to manually perform tests every time you make changes for every area of code affected. Just thinking about it makes many people want to slow down and reduce the pace of code updates, which is exactly the opposite of what most software teams need to achieve. How often have you seen a desire to introduce more benefits and improvements into the software projects as quickly as possible? Probably all the time.
Furthermore, automating unit testing in your software project means that any issues that are found while the test suite runs are likely freshly introduced, and detecting them right away makes it easier to go into the code and fix. Not being able to detect an issue for a long time usually means the developer who was working on that area of code has already moved on to other work and would need to go back and re-learn how the code works and what changes were made to design the best solution to the issue.
There is a long list of other benefits to automated testing covered in our other posts, though these are some of the highlights in the context of unit testing.
The most popular frameworks
Part of the popularity of Jest is that it covers a lot of prevalent technologies and frameworks. Developed by Facebook and based on Jasmine, it’s no surprise that Jest is especially preferred for projects using React. However, Jest is also much more broadly popular and supports unit testing for Angular, VueJS, NodeJS, etc.
Additionally, Jest users find it easy to use as well as appealing that it requires zero setup to get started. Setup basically comes down to using npm or yarn to run a single command to install or add Jest to your project. The snapshot capturing feature is also a big plus, as it makes it fairly easy to manage large test cases.
When it comes to performance, some teams swear by the speed of Jest. In one prominent case from Airbnb, switching from Mocha to Jest yielded a drop from 12 minutes to 4.5 minutes in the total runtime of test cases on a 32 core system. Jest accomplishes this level of speed in part by running test cases in parallel and running the slowest test cases first to ensure that CPU cores are all maxed out to eliminate wasteful delays.
Other widespread benefits of Jest include:
- All of the features included in Jasmine, which Jest is based on
- Spies that record how a function under test is used
- Built-in matchers that let you test values in different ways
- Support for TypeScript, which is a big help for Angular testing
- Visual regression testing with the snapshot capture feature
- A watch mode feature that runs relevant automated test cases whenever code is changed
- Test coverage reports so you can identify what percentage of coverage exists for your project and where your project is covered – all with just a single command
- Extensive and easy-to-use mocking to help replace slow dependencies, including timer mocks, Ecma 6 script class mocks, and manual module mocks.
Cypress also comes with a test runner that produces automated screenshots and video captures if desired.
At the heart of Storybook is the ability to create independent components to facilitate unit testing, which makes it easy to then showcase the components in an isolated development environment.
In addition to making unit testing easier, Storybook also helps facilitate documentation of test cases and guidelines, which can help other team members working on the project, especially those who are new to the project.
Much of the popularity of Puppeteer can be attributed to a wide range of features, including:
- Support for ES6 features like async and await
- Automated testing for many types of interactions like keyword input and form submit
- Browser-specific tests that can facilitate responsive testing by adjusting browser size and resolution.
A popular testing library for Node.js, AVA is often preferred for the strong features allowing for process isolation that allows each test file to run in a separate Node.js process. This process isolation can help reduce the potential for interference across test cases, making test outputs more reliable and faster to execute as they run concurrently.
In addition, AVA boasts a simple syntax, enforces the writing of atomic tests, and provides a magic assert feature with assertion differences that are easy to understand.