Unlock the Power of Testing: A Step-by-Step Guide to RESTful API Testing with Strapi

Why Testing Matters

When it comes to building applications, testing is often an afterthought. However, it’s a crucial step in ensuring the quality of your finished product and uncovering errors. In this tutorial, we’ll dive into the world of testing RESTful APIs built with Strapi, an open-source headless JavaScript CMS.

Getting Started with Strapi

To begin, you’ll need to install Strapi using the following command: npx create-strapi-app strapi-mocha-chai --quickstart. This will initialize a Strapi project with an SQLite database. Once installed, navigate to http://localhost:1337/ in your browser to view the application.

Creating Endpoints

Let’s create a to-do application with the following endpoints:

  • POST /todos: Create a new todo
  • GET /todos: Get a list of all todos
  • GET /todos/:id: Get a single todo
  • PUT /todos/:id: Update an existing todo
  • DELETE /todos/:id: Delete an existing todo

To create these endpoints, we’ll need to create a content type, similar to a database definition. Follow these steps:

  • Go to “Plugins” and click “Content Types Builder”
  • Click “Create new collection type” and enter “Todos” as the display name
  • Add fields for “title” and “completed”
  • Save the content type

Setting Up the Test Environment

Before we begin testing, we need to create a separate database config for our tests. Run the following command: node ace configure:database --env=test. This will create a database.json file in the ./config/env/test/ path.

Next, create an instance of the Strapi server before running the tests. Paste the following code into your terminal:

“`javascript
const { Strapi } = require(‘@strapi/strapi’);

async function startStrapi() {
const strapi = new Strapi();

await strapi.load();
await strapi.bootstrap();

return strapi;
}

module.exports = startStrapi;
“`

Testing the Strapi Instance

Create a file called app.test.js in the /tests/ directory and paste the following code:

“`javascript
const { expect } = require(‘chai’);
const { startStrapi } = require(‘../helpers/strapi’);

describe(‘Strapi instance’, () => {
let strapi;

before(async () => {
strapi = await startStrapi();
});

after(async () => {
await strapi.destroy();
});

it(‘should initialize Strapi’, async () => {
expect(strapi).to.exist;
});
});
“`

Testing the Endpoints

Create a file called index.test.js in the /tests/todo/ directory and paste the following code:

“`javascript
const { expect } = require(‘chai’);
const { startStrapi } = require(‘../../helpers/strapi’);
const axios = require(‘axios’);

describe(‘Todos endpoints’, () => {
let strapi;
let axiosInstance;

before(async () => {
strapi = await startStrapi();
axiosInstance = axios.create({
baseURL: ‘http://localhost:1337’,
});
});

after(async () => {
await strapi.destroy();
});

it(‘should create a new todo’, async () => {
const response = await axiosInstance.post(‘/todos’, {
title: ‘New todo’,
completed: false,
});

expect(response.status).to.equal(201);

});

// Add tests for the remaining endpoints
});
“`

Running the Tests

To run the tests, open up your terminal and run yarn test. You should get results similar to the screenshot below.

The Importance of Testing

Testing is an integral part of the software development lifecycle. By following this tutorial, you’ve taken the first step in ensuring the quality of your Strapi application. Remember, testing is not a one-time task, but rather an ongoing process that requires continuous effort.

Leave a Reply

Your email address will not be published. Required fields are marked *