Follow

Follow
Integration Testing with Jest

Integration Testing with Jest

Collins Mbathi's photo
Collins Mbathi
·Jan 25, 2023·

9 min read

Play this article

Introduction

Application testing is important because it ensures that the code is working as it should and that any changes made to the code do not break existing functionality. Writing tests allows developers to quickly and easily identify and fix bugs, saving time and resources in the long run. Furthermore, testing can help to improve the overall quality and maintainability of the codebase, making it easier to understand and work with for other developers. In our article, we are going to focus on integration testing with jest.

Jest is a well-known JavaScript testing framework for performing integration tests on web applications.

This article will provide an overview of the framework and its features, as well as a step-by-step guide on how to set up and run integration tests with Jest. It will also cover some best practices and tips for writing effective Jest integration tests. Overall, this article will provide a comprehensive guide to Jest integration testing and will assist developers in ensuring the proper operation of their web applications.

That’s it let's get started,

What Is API Testing, and Where Can We Use It?

API (Application Programming Interface) testing is a type of software testing that involves testing APIs directly and as part of integration testing to see if they meet functionality, reliability, performance, and security expectations. API testing can be done at three levels: unit, functional, and end-to-end. It can be used to test the functionality of a single API, the performance of a group of APIs, or the end-to-end functionality of a multi-API application. It can be used in a wide range of software development projects, including mobile apps, web apps, and microservices.

What is Integration Testing? will be discussed in the following section.

What Is Integration Testing?

Integration testing is a type of software testing that examines how various components of a system interact with one another. This is accomplished by combining individual units of code and testing them as a group as opposed to testing each unit individually. The goal of integration testing is to identify and resolve any issues that may arise when the various components of a system are combined.

Prerequisites will be discussed in the following section.

Prerequisites

  • Node.js and npm (or yarn) are installed on your machine.

  • A Node.js API or application that you want to test.

  • Jest and the jest-cli package are installed as a dev dependency in your project. You can do this by running npm install --save-dev jest jest-cli

  • Understanding of JavaScript.

  • Understanding of the API you are testing and its endpoints.

Writing Integration Tests

This section will teach you how to write integration tests as well as some of the fascinating features that Jest provides to make testing easier.

Before we begin writing tests, consider the following:

  • Making a new post

  • acquiring all posts

  • Getting a post by id

  • Changing the information in a post

  • Removing a post

Create the endpoints for the above-mentioned logic, test the API endpoints with Postman, and then return to continue writing the test part.

Writing Test with Jest

To begin using Jest for integration testing in a Node.js API, you must first install the Jest package. This can be accomplished by issuing the following command:
npm install --save-dev jest

After installing the package, we need to configure our tests to use Jest. In your package.json, replace the following section:

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},

With:

"scripts": {
"test": "jest"
},

We also require another configuration file; create a file in the project root called jest.config.js and add the following code:

{
"preset";"@shelf/jest-mongodb"
}

Supertest is another important module that works hand in hand with Jest. It will greatly simplify testing our endpoints. As a result, we install it as follows:

npm install  --save-dev supertest

Setting up the Test Environment

Create a folder in the root directory and name it __test__. Inside that folder create a file and name it testDBSetup.js

We're creating the database, but we'll use mongoose-mock to mock Mongoose and test your API with real requests without touching the real database.

Install Mongoose and the mongoose-mock package by running the following command:

npm install mongoose mongoose-mock

Inside the testDBSetup.js insert the following code :

const mockgoose = require('mockgoose');

mockgoose(mongoose);

In your Jest configuration file, set up the setupFilesAfterEnv property to include the mongoose file:

"setupFilesAfterEnv": ["./__test__/testDBSetup.js"]

You can now use Mongoose in your tests as usual, and all database operations will be mocked. You can, for example, create a new Post model:

const post = mongoose.model('Post', { title: String, content: String });

You can then use your test database to create, read, update, and delete records without having to connect to a real MongoDB cluster. When writing tests with Jest, you should be familiar with the following terms and concepts:

Test Suites: A collection of test cases that are grouped. In Jest, a test suite is created using the describe() function.

Test Cases: An individual test that checks a specific behavior of your code. In Jest, a test case is created using the test() function.

Assertions: Statements that check if the outcome of a test case matches the expected outcome. Jest provides several built-in assertion functions, such as expect(), toBe(), and toEqual().

Mocks: A way to replace a specific function or module with a test implementation. This allows you to test how your code behaves in specific conditions or scenarios. In Jest, mocks are created using the jest.fn() function.

Setup and Teardown: The ability to run specific code before and after each test case. In Jest, you can use the beforeEach() and afterEach() functions to set up and tear down your test environment.

Asynchronous tests: Tests that involve code that runs asynchronously, such as promises or callbacks. In Jest, you can use the async keyword and the done callback to handle asynchronous tests.

By understanding these terms and concepts, now you can write effective tests.

1. Create a Post Endpoint

Make a file called addPost.test.js inside the __test__ folder. insert the following code here :

describe('POST /posts', () => {
it('creates a new post', async () => {
// create test data
const post = {
title: 'Test post',
content: 'This is a test post.'
};
// make a POST request to the /posts endpoint
const res = await request(app).post('/posts').send(post);
// assert that the response status is 201 (created)
expect(res.status).toBe(201);
// assert that the response body contains the test post data
expect(res.body).toEqual({

_id: expect.any(String),

title: post.title,

content: post.content

});
// check if the post was created in the database
const createdPost = await Post.findById(res.body._id);

expect(createdPost).toEqual(expect.objectContaining(post));

});

});

In the preceding test case, we send a POST request to the create post endpoint with sample post data. The response is then claimed to be the same as the data sent. This is how the API endpoint works in practice. We can also see that the status code is 201, which indicates success.

2. Get all the Posts Endpoints

Make a file called getPosts.test.js inside the __test__ folder. insert the following code here :

it('should return all posts', async () => {

const res = await request(app).get('/posts');

expect(res.status).toBe(200);

expect(res.body).toBeInstanceOf(Array);

});

A GET request that expects all of the database's test posts to be returned.

3. Get one Post Endpoint

Make a file called getPost.test.js inside the __test__ folder. insert the following code here :

it('should return a specific post', async () => {

const post = await request(app)

.post('/posts')

.send({ title: 'Test Post', content: 'This is a test post.' });

const res = await request(app).get(`/posts/${post.body._id}`);

expect(res.status).toBe(200);

expect(res.body.title).toBe('Test Post');

expect(res.body.content).toBe('This is a test post.');

});

In this case, we make a GET request with a specific post id and then verify that the response contains the correct data.

4. Update a Post Endpoint

Make a file called updatePost.test.js inside the __test__ folder. insert the following code here :

it('should update a specific post', async () => {

const post = await request(app)

.post('/posts')

.send({ title: 'Test Post', content: 'This is a test post.' });

const res = await request(app)

.put(`/posts/${post.body._id}`)

.send({ title: 'Updated Test Post' });

expect(res.status).toBe(200);

expect(res.body.title).toBe('Updated Test Post');

});

In this test case, we send a PUT request to update the details of a specific post. We then expect that the response will include the updated post data.

5. Delete a Post Endpoint

Make a file called deletePost.test.js inside the __test__ folder. insert the following code here :

it('should delete a specific post', async () => {

const post = await request(app)

.post('/posts')

.send({ title: 'Test Post', content: 'This is a test post.' });

const res = await request(app).delete(`/posts/${post.body._id}`);

expect(res.status).toBe(200);

expect(res.body._id).toBe(post.body._id);

});

});

Testing the delete endpoint is also simple; all we need to do is send a DELETE request to the delete endpoint and verify that we receive the correct response.

That's all. We were able to write tests for all of the endpoints. If you have already set up the application, npm run test to see if it works.

Here is the result after running it :

What Is the Best Approach to Rest API Testing?

The best way to test REST APIs is to use a combination of manual and automated testing.

Manual testing entails sending requests to the API and manually verifying the responses with tools such as Postman or curl. This is useful for quickly testing the API's basic functionality and exploring its features.

Automation testing entails creating scripts or programs that send requests to the API and validate the responses. This is useful for regression testing, ensuring that new API changes do not break existing functionality.

When testing your REST API, keep the following points in mind:

1. Test at least the most important API endpoints

2. Test the API with a variety of inputs to ensure it can handle a variety of scenarios.

3. Verify that the API's error handling returns meaningful error messages and HTTP status codes.

4. Evaluate the API's performance and scalability to ensure that it can handle a high volume of requests.

It's also a good idea to use a testing framework like Jest.

Conclusion

Jest is an effective tool for integration testing as it has several features, such as asynchronous testing, mocking, and snapshot testing, that make it ideal for testing the interactions between different components or modules in your application. Jest is also popular among developers due to its simple syntax and built-in assertion library.

This article has covered the following topics:

1. What exactly is integration testing?

2. How to Install and Configure Jest for Integration Testing

3. Writing and running integration tests with Jest

I hope this article has helped you understand integration testing with Jest and how you can use it to improve the overall quality of your application.

 
Share this