Boaz Poolman
Published on 20 January 2025
In the first article of this series, we explored setting up a playground instance to facilitate automated testing for Strapi plugins. Building upon that foundation, this article focuses on validating the API endpoints of your Strapi plugin to ensure they function as intended.
Prerequisites:
To get up to speed I recommend you read the previous article first. You can find 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