Strapi

Automated Testing for Strapi v5 Plugins: Using a Playground Instance

Boaz Poolman

Published on 2 January 2025

Automated testing is essential to ensure the quality and reliability of your Strapi plugins. A playground instance can help you test your plugin in a controlled Strapi environment without relying on external projects. This article explains how to set up and integrate a playground into your plugin repository.

What is a Playground Instance?

A playground instance is an isolated, easily configurable environment within your plugin repository. It provides a realistic Strapi environment where you can test and debug your plugin, making it easier to develop and validate your work.

How Does the Playground Work?

To demonstrate how this works, I've created a Plugin Boilerplate, which can be used as a template for new plugins. By browsing the repository, you will find the following:

 

Strapi Instance in /playground:

The playground directory contains a basic Strapi configuration, including files like config, extensions, and data. This instance is directly connected to the plugin in the repository’s root directory, allowing you to test plugin changes without setting up a separate Strapi app.

 

Plugin Integration with the Playground:

The plugin is set up using @strapi/sdk-plugin, which provides the commands to build and watch the source code. Using these commands, the plugin will be pushed to a local yalc registry, making the plugin directly available within the playground without needing a published package.

 

More information about this can be found in the Contributing Guidelines.

 

Testing in the GitHub Actions Pipelines:

The playground is used in the GitHub Actions pipeline to test the plugin in a Strapi application. By doing this, we can validate that Strapi still works after installing our plugin.

 

By running the playground in the pipeline, we also set ourselves up for conducting more specific integration and end-to-end (e2e) tests in the future.

Setting Up a Playground for Your Plugin

If you’re developing your own Strapi plugin, you can use our Plugin Boilerplate as a template. It has been set up in the following way:

 

1. Initiate a New Plugin

npx @strapi/sdk-plugin@latest init my-plugin

After you've successfully initiated your plugin and navigated to its root, you can proceed to the next step:

 

2. Initiate a New Strapi Application

npx create-strapi@latest playground

3. Install Plugin Dependencies

yarn install

4. Build the Plugin with watch:link

yarn watch:link

After you've successfully built the plugin, navigate to the playground and proceed to the next step:

 

5. Install the Plugin in the Playground

yalc add strapi-plugin-boilerplate

6. Install Playground Dependencies

yarn install

7. Enable the Plugin

In the config/plugins.ts file of the playground, enable the plugin:

export default () => ({
  boilerplate: {
    enabled: true,
    config: {},
  },
});

And that's it! You can now use the playground for testing and development. For more specific details about the development workflow, refer to the Contributing Guidelines of the Plugin Boilerplate.

Testing the Playground in the Pipeline

All the steps you've followed above can be reproduced in a pipeline. By doing so, we can validate that Strapi still works after installing our plugin.

 

You can take the GitHub Actions configuration of the Plugin Boilerplate as an example.

Conclusion

Setting up a playground instance simplifies Strapi plugin development by providing a controlled environment for testing and debugging. It ensures faster iterations, robust validation, and seamless integration into your GitHub Actions pipeline, improving both efficiency and reliability.

 

Next Steps

  • Add custom integration and end-to-end (e2e) tests for advanced validation.
  • Expand the playground's capabilities to cover edge cases and complex scenarios.
  • Upload code coverage to Codecov.

Boaz Poolman

Published on 2 January 2025

More of Strapi