【My Study Note】Introduction to Jest
Introduction to Jest
Jest is a testing framework for JavaScript.
JavaScript doesn’t have built-in objects or methods that would allow for tests to be written. Therefore, many different libraries/frameworks have been built to tackle the issue of testing.
Some examples
- Jasmine
- Mocha
- Karma
- qUnit
- Jest
What Jest allows you to test
- Plain JavaScript Code
- React code
- Babel
- TypeScript
- Node
- Angular
- Vue
- various other frameworks.
Code Coverage
Jest also supports code coverage. Code coverage measures what percentage of your code is covered by tests. If you say that you have 80 percent code coverage, that means that only one-fifth of your entire code base is not covered by tests.
But even 100 percent code coverage doesn’t mean that you have tested for every conceivable expectation. It just means that there are some expectations tested for each line of your code. Still, code coverage is a handy tool to gauge the amount of your code base that’s included in tests. The higher the code coverage, the lower the chance of having unidentified bugs.
As a rule, the higher the code coverage percentage, the less time is required to write new tests.
This, however, depends on whether there are incomplete software requirements pending or if you are going to receive more requirements in the future.
Mocking
Mocking allows you to separate the code you are testing from its related dependencies. In other words, you can use the mocking features to make sure that your unit testing is stand-alone.
For example, you can test the front-end functionality of your web app by mocking the data as if it came back from a server when in fact it came from the client.
Mocking is especially helpful because very often web applications are built by teams of developers. Some developers work on the backend of a feature and others work on the front end. This could result in bottlenecks.
Take an example where the team decides to build a new feature that lists the address book of users of the app on the front end. The actual user-related data for this feature would come from the server.
But what if a back-end developer was a bit late in developing their part of the feature? Then a front-end developer would be stuck waiting for the back-end developer to complete their work before the front-end code can be built.
With mocking, you can avoid this bottleneck. Mocks, allow you to pretend that the users are already there. The needed data comes from the mock rather than from the backend.
This allows the front-end developers to finish their site of the new feature independently. In certain cases, developers can use mocking to ship features faster.
Some libraries, such as sign-on, focus specifically on mocking. But the great thing about Jest is that you use its mock functions without any additional installations.
In Jest, you use mocking by employing Jest mock functions. It’s also easy to test asynchronous code in Jest. There are no difficult setups and tests are relatively easy to code even for newcomers to the framework.
Snapshot Testing
Snapshot testing is used by developers to verify that there are no regressions in the DOM of our apps after some changes to the code base are made.