What Is Jest?
Jest is an open JavaScript testing library from Facebook.
It is mainly used for white box (unit/integration) testing purposes, but it can also be utilized for black box testing techniques (API testing, UI testing..etc.). It has good cross-browser compatibility and is widely used with Selenium for automated testing. It has recently gained much popularity for both front-end and back-end testing.
Jest is essentially a framework rather than a library. There’s even a command-line interface (CLI) tool available. For instance, you can use the CLI tool to execute only those tests that meet a pattern. Aside from that, it has many more features, which you may read about in the CLI documentation.
This means that Jest offers a test runner, assertion library, CLI tool, and great support for different mocking techniques.
Describe Blocks
A test suite is defined via a describe block. A test suite is a collection of one or more tests that are functionally related.
describe('verify something', () => { //your code here });
The describe
block is used to group test cases into logical categories. For instance, suppose we wish to collect all of the tests for a particular class. New described blocks can be nested within an existing described block.
To keep the example going, you can add a describe
block that wraps all tests for a specific function in this class.
“It” or “Test” Tests
A single test is described by an it
block. A test is a single functional unit that you want to put under inspection.
In addition, the test keyword is used to begin a new test case specification. It
keyword is just another way of saying test, I prefer to use it since it provides for a more natural linguistic flow when developing tests.
it('Validate test', async() => { //your test here });
Using it block, we can separate the various tests used in the described block that are part of one test suite/test scenario. In this way, we have a better insight into the running tests, and in the test report itself, we have an overview of which part of the test failed.
Assertions/Matchers
Let’s have a look at the matchers that Jest reveals next. We use expect keyword to make an assertion. We want to compare the result of our test to a value that we anticipate the function returning.
it('Validate test', async() => { expect(locator()).toBe('available'); });
With the expect()
keyword, as the name suggests, we expect a certain output to match the desired one. We use expect in combination with matcher, and general syntax would be
expect(actualValue).matcher(expectedValue)
Setup and Teardown
It’s critical that we know how to prepare for and clean up a test. Let’s say we use a database, for example, in one of our tests. We don’t want to run a function for each test to set up and clean up the database.
To avoid code duplication, we can use the beforeAll
and afterAll
hooks to fix this problem. You can use both functions to run logic before or after each test suite.
Running functions before and after the test can be helpful in automated testing, from loading data from the database to complex setup scripts, as well as cleaning up after tests. The beforeAll
and afterAll
hooks enable this.
With beforeAll
, we can predefine all the data we need in the test suite and load it before starting tests.
With the help of afterAll
, we can write functions that will be executed after all tests, in most cases, it is used to clean (delete) the data used in the tests.
Similar to beforeAll
and afterAll
, we also have beforeEach
and afterEach
The main difference compared to beforeAll
and afterAll
is that these hooks are used when we want to run specific functions before and after each test within the test suite. Unlike beforeAll
and afterAll
, which are executed once before the tests and once after all the tests in the test suite, these hooks are run multiple times, depending on the number of tests.
It is important to note that the functionality of beforeEach
and afterEach
also depends on their place in the test suite itself.
If, for example, we set beforeEach
before the Describe block, that hook will be executed once before each Describe block, if we put it inside the describe block, before the It block, beforeEach
will be executed before each it
block independently.
Finally, besides using beforeAll
/ beforeEach
, afterAll
/ AfterEach
blocks, we can write our custom setup and teardown scripts which will be executed before all and after all test suites we have.
We can control the order of their execution in various ways, one of them is by defining the npm script (package.json), which defines the order.
These scripts are used in beforeAll
and afterAll
hooks.
This allows us to place the desired data in a database we will use later in the defined tests. In this way, we can also load users into the database via APIs and all the data we want to test.
This avoids unnecessary duplication of processes we would have to define for each test suite.
For example, if we have a set of users who use certain data in the system, we can perform different tests for the same users without loading them every time the test suite is run. This is especially useful in sequential execution.
Testing asynchronous code
To test asynchronous code, we can use async and await functions. Use the async keyword in front of the function supplied to it to create an async test.
it('Validate test, async() => { let response = await api.response(); expect(response).toEqual(userData); });
Jest has all the building blocks needed to ensure you can quickly and efficiently write your test specs, no matter what type of testing you are doing.
If you liked this blog, read more about QA/Test Automation.