Write Integration Tests (and Mocks) with Jest and Supertest

Write Integration Tests (and Mocks) with Jest and Supertest

In this article, we are going to discuss how we can write integration tests using Jest, Supertest and in process of writing integration tests, we often require mocking npm modules and dependencies, so we will also mock these npm modules and dependencies for smooth and complete testing.

In this article, we are going to discuss how we can write integration tests using Jest, Supertest and in process of writing integration tests, we often require mocking npm modules and dependencies, so we will also mock these npm modules and dependencies for smooth and complete testing.

Image for post

Photo by Abet Llacer from Pexels

Let’s get started, Jest is an excellent library probably one of the best to write tests, check test coverage, mock modules, and is equipped with a lot of other useful features. First, we will create a Node.js application on which we can write tests.

const express = require('express');
const app = express();
const PORT = process.env.PORT || 8000;
const jwt = require('jsonwebtoken');
const secretAccessKey = process.env.secretAccessKey || 'SecretAccessKey';

// To mock any NPM module make sure that you are not destructuring them while requiring them in your file.
// For example - If we use 
// const {verify} = require('jwt'); in place of const jwt = require();
// Jest would not be able to mock destructured modules. So, always keep in mind while writing your code to not to use destructured imports or require.

app.get('/verify-access-token', (req, res) => {
    const accessToken = req.headers['access-token'];
    try {
        jwt.verify(accessToken, secretAccessKey);
        return res.status(200).send({ message: 'Access token is verified' });
    } catch (err) {
        return res.status(401).send({ message: 'Unauthorized access' });
    }
});

app.listen(PORT, () => {
    console.log(`Server started at port ${PORT}`);
});

module.exports = app;

tdd jest testing javascript programming

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Building A Test Runner in Jest - A How-To Guide

I use Jest nearly every day when working, and it's a fantastic tool. It lets me ship my code with confidence, knowing that I have produced something which works as intended.

Jest For Selenium JavaScript Testing (With Examples)

As a developer, I know for a fact that I have to constantly brush up myself with new updates. This means that not only I have to keep learning more about the frameworks and language I work with but also to look for new frameworks that gives me an edge over others. One such survey that I am always looking forward to as a developer is the ‘StateofJS’. It is revered by all the JavaScript developers as a key source of information. ‘StateofJS’ provides key insights on the major trends in front-end, back-end, testing, etc. As per StateofJS 2019, Jest has been the most interesting and satisfying framework for JavaScript testing.

Jest Tutorial for Beginners - JavaScript Testing with Jest Framework

In this Jest tutorial for beginners, you'll learn how to getting started with JavaScript unit testing with Jest framework. What means testing? How to test JavaScript code with Jest? Learn the basics of testing JavaScript with this Jest tutorial for beginners!

Easy JavaScript unit tests in WordPress with Jest

Ensure the stability of the JavaScript code in your WordPress plugin by writing JavaScript unit tests using Jest. WordPress has a long record of unit testing its PHP code. However, writing JavaScript unit tests and integration tests for the code in themes and plugins doesn’t enjoy the same status. Let’s see how to test the JavaScript in our WordPress plugins and themes using Jest.

Unit Testing With Jest in Javascript

I decided to spend today learning the ins and out of using Jest and creating test cases so that I may add unit tests to my projects.