Why should I write tests?
As software developers, it is our prime responsibility to ensure that we ship apps that are of high quality. Now, how do you know if your app passes the quality test? Well, writing tests to validate the app if one of the fundamental tasks that we need to do as software developers.
“Quality is never an accident; it is always the result of intelligent effort.” – John Ruskin
Often times, we tend to skip writing tests for our code. And we may have plenty of reasons like meeting deadlines, client doesn’t care about tests, and so on. I believe, writing tests needs to be a part of our everyday job, and we should integrate it within the process.
Alright, enough said about the importance of testing.
What is Jest?
If you are someone who hates writing tests, Jest will change your opinion. It is super easy to use and developers love writing tests with Jest.
Tell me more, what is cool about Jest?
Jest makes writing mock functions quite simple. It uses custom resolver for imports in your tests, hence making it easy to mock any object that is outside of your test’s scope.
Jest uses snapshots that can make your test keep track of large objects in an organized manner. Snapshots live either alongside your tests, or are embedded inline. This is useful, if you have a huge object stored in the redux store, and want to validate it’s data.
Tests run in Parallel
This is one of the distinguishing feature of Jest, making it suitable for fast paced development. The tests run parallely, in isolation. Since they have their own processes, it maximizes the performance.
Since tests run in parallel, Jest is a fast option for testing. It also runs previously failed tests first, and re-organizes test runs based on how long test files take.
Jest APIs are documented clearly and are easy to use. You can browse through the Jest APIs to get an idea on how to get started.
If you are interested to learn more about Jest, the talk below from the core team will provide a lot of insights.
Jest can generate code coverage by simply using the –coverage command. No additional setup is needed to generate the coverage report. This can be used to validate your test cases, and predict how many more test cases are needed to achieve better coverage.
Snapshot Testing with Jest
Snapshot testing ensures that your UI does not change in an unexpected manner. The idea is simple. An initial snapshot of UI component is taken, and it is compared to it’s reference snapshot file stored in the test. The test will fail, if the two snapshots don’t match. This will indicate to us that either, the change is unexpected, or the reference snapshot needs to be updated to the latest version of the UI component.
These types of testing are really common while testing mobile applications.
Jest supports snapshot testing your React/React Native components. You may think, it could be a hassle to constantly update the reference snapshots as your UI changes. Jest has this process simplified for you. You would just need to run one command to update the snapshots and the new reference snapshot replaces the old one.
Here is more information from the official blog, on snapshot testing using Jest.
Moving to Jest from an existing codebase
Not everyone is starting a new project all the time. In reality, if you are on an existing codebase with another testing framework and would like to migrate to Jest, it is quite simple.
- If you are on Jasmine or Mocha, Jest is compatible with these frameworks and the migration is simple.
- For other frameworks, you can use the codemods to transform your tests automatically.
Migrating existing tests with jest-codemods
Install the third party plugin, jest-codemods. It can be used to handle all the work to migrate these tests.
yarn global add jest-codemods
To transform your existing tests, navigate to the project containing the tests and run:
Who is using Jest?
Jest is used today by many fortune 500 companies to test their products.
It was originally developed by Facebook and has been open sourced for the community to use and contribute to.
If you are looking for a solid course on React, Mosh has that covered too.