Testing REST APIs with Postman and Jest

Tutorial 5 of 5

1. Introduction

In this tutorial, we aim to demonstrate how to test REST APIs using Postman and Jest.

You will learn:

  • How to write tests using Jest
  • How to send requests to your API using Postman
  • How to validate responses using assertions

Prerequisites:

  • Basic understanding of JavaScript
  • Familiarity with REST APIs
  • Node.js installed on your system
  • Postman installed on your system

2. Step-by-Step Guide

Firstly, we will start with Jest. Jest is a JavaScript testing framework maintained by Facebook. It's great for testing JavaScript and React applications.

Install Jest

You can install Jest using npm (node package manager) with the following command:

npm install --save-dev jest

Writing a Test

In Jest, each test is written as a function which takes a name and a function containing the logic for the test.

test('test name', function() {
  // test logic here
})

Postman

Postman is a platform that allows us to test, develop and document APIs. It provides a user-friendly interface to send HTTP requests and view responses.

Sending a Request

To send a request in Postman:

  1. Select the HTTP method from the dropdown (GET, POST, PUT, DELETE, etc.)
  2. Enter the URL of the API endpoint
  3. Click on the "Send" button

You can add query parameters, headers, and body data as needed.

3. Code Examples

Example 1: Simple Jest Test

// Import the http module
const http = require('http');

// Import the jest-fetch-mock module
const fetchMock = require('jest-fetch-mock');

// Enable fetch mocks
fetchMock.enableMocks();

// Test
test('checks if Jest works', () => {
  expect(1).toBe(1);
});

// Test API
test('User fetch successful', async () => {
  fetchMock.mockOnce(JSON.stringify({ name: 'John' }));

  const response = await fetch('/users/1');
  const user = await response.json();

  expect(user).toEqual({ name: 'John' });
});

In the above example, we first test if Jest works by checking if 1 is equal to 1. Then we mock a fetch request and check if it returns the correct data.

Example 2: Sending a Request in Postman

Let's say you have a GET request for 'http://localhost:3000/users/1'.

  1. Select 'GET' from the dropdown
  2. Enter 'http://localhost:3000/users/1' in the URL field
  3. Click 'Send'

In the response section, you should see the user data returned from the server.

4. Summary

In this tutorial, we learned how to install and write tests using Jest. We also learned how to send requests to an API using Postman.

Next, you should learn more about different types of testing like unit testing, integration testing, and end-to-end testing. You should also learn more about different HTTP methods and status codes.

Here are some resources for further reading:

5. Practice Exercises

Exercise 1:

Write a test for a function that adds two numbers.

Solution:

test('adds 1 + 2 to equal 3', () => {
  function add(a, b) {
    return a + b;
  }
  expect(add(1, 2)).toBe(3);
});

Exercise 2:

Send a POST request in Postman to 'http://localhost:3000/users' with the following body data:

{
  "name": "John",
  "email": "john@example.com"
}

Solution:

  1. Select 'POST' from the dropdown
  2. Enter 'http://localhost:3000/users' in the URL field
  3. Go to the 'Body' tab, select 'raw' and 'JSON'
  4. Enter the body data
  5. Click 'Send'

For further practice, try writing more complex tests and sending different types of requests in Postman.