Boaz Poolman
Published on 20 January 2025
The most important thing to test in your Strapi Plugin is probably the API's. I'm talking about the REST endpoints that it provides, but also the services it exposes. Pretty much anything in the `/server` folder of your Plugin is what we're looking to test.
To do so we're going to make use of the popular testing library Jest, together with Supertest for making HTTP requests
Prerequisites:
To get up to speed I recommend you read my other article, explaining how to setup a Playground instance for your plugin. You can read it here:
A quick note before we start: the setup that I'll be explaining in this article is available as a template on Github. If you want to get right in to it, or prefer exploring a Github Repository rather than reading an article, please refer to the Strapi v5 Plugin Boilerplate. It has been setup with all the automated testing goodness and is ready for you to start building!
Now on with the article.
To begin, ensure that Jest and Supertest are installed as development dependencies in your plugin's root directory:
This command installs Jest for running tests and Supertest for making HTTP assertions.
In your plugin's package.json file, add the following script to facilitate running tests:
Additionally, create a file called jest.config.js with the basic Jest configuration:
This setup ensures that Jest uses the Node.js environment and ignores unnecessary directories during testing.
To test your plugin's API endpoints, initialize the Playground instance within your test suite. Create a tests directory in the playground's root and add a helpers.js file with the following setup:
This script provides functions to set up and tear down the Playground instance for testing purposes.
With the setup complete, you can now write tests to validate your plugin's API's. Assuming your plugin has been setup with @strapi/sdk-plugin, it will have an example API endpoint provided by default. Create a tests directory in the root of your repository, and add an example.test.ts file and write your first test:
This test suite initializes the Playground instance before running tests and cleans up afterward. The test checks that a GET request to the /api/boilerplate endpoint returns a 200 status code and a response body containing the value 'Welcome to Strapi 🚀'.
Note that this test is written for a plugin called 'boilerplate'. Please replace 'boilerplate' with the identifier of your plugin.
Execute the tests by running the following command in your plugin's root directory:
Jest will run the test suite, and you should see output indicating whether the tests passed or failed.
All the steps you've followed above can be reproduced in a pipeline. By doing so, we can validate that the API's of your plugin still work after each change in the codebase.
You can take the GitHub Actions configuration of the Plugin Boilerplate as an example.
By following these steps, you've set up automated testing for your Strapi plugin's API endpoints using Jest and Supertest. This approach ensures that your plugin's APIs function correctly, enhancing the reliability and robustness of your plugin.
In the next article, we'll delve into more advanced testing strategies, such as integration and end-to-end testing, to further solidify your plugin's quality assurance processes.
Boaz Poolman
Published on 20 January 2025