Testing API routes in Next.js

Tutorial 5 of 5

1. Introduction

What is the goal of this tutorial?

This tutorial aims to provide you with detailed steps on how to test API routes in Next.js. API testing is a critical part of web development, as it helps ensure that your server-side functions are returning the expected data and handling errors correctly.

What will you learn?

By the end of this tutorial, you will be able to:
- Understand how to use Jest and Supertest for API testing in Next.js
- Write tests for your API routes
- Understand how to interpret test results

Prerequisites

A basic understanding of Next.js, JavaScript, and API routes is required. Familiarity with Jest and Supertest would be beneficial but is not mandatory.

2. Step-by-Step Guide

Testing API routes in Next.js involves several steps. Here's a step-by-step guide:

Install Dependencies

First, install the necessary testing libraries, Jest and Supertest, by running the following commands:

npm install --save-dev jest supertest

Create a test file

Next, create a test file. For example, if your API route file is pages/api/hello.js, you could create a test file named tests/hello.test.js.

Writing Tests

In your test file, you'll use Jest's syntax to write tests. Supertest will be used to make requests to your API routes.

3. Code Examples

Here's an example of a test for a simple API route:

pages/api/hello.js

export default (req, res) => {
  res.status(200).json({ text: 'Hello' })
}

tests/hello.test.js

const request = require('supertest')
const server = require('../pages/api/hello')

describe('Hello API route', () => {
  it('should return a greeting', async () => {
    const response = await request(server).get('/api/hello')

    expect(response.statusCode).toBe(200)
    expect(response.body).toEqual({ text: 'Hello' })
  })
})

In this test, we're sending a GET request to our /api/hello route and expecting a 200 status code and a specific response body.

4. Summary

In this tutorial, we've learned how to test API routes in Next.js using Jest and Supertest. We've learned how to write tests, run them, and interpret the results.

If you want to learn more about API testing, consider studying more about Jest and Supertest.

5. Practice Exercises

To further cement your understanding, try out the following exercises:

  1. Write a test for an API route that returns a 404 status code.
  2. Write a test for an API route that accepts POST requests and returns the data sent in the request body.

Solutions

  1. The test might look something like this:
it('should return a 404 status code', async () => {
  const response = await request(server).get('/api/nonexistent')

  expect(response.statusCode).toBe(404)
})
  1. For the second exercise, you might write a test like this:
it('should echo the request body', async () => {
  const requestBody = { text: 'Hello' }
  const response = await request(server).post('/api/echo').send(requestBody)

  expect(response.statusCode).toBe(200)
  expect(response.body).toEqual(requestBody)
})