npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

strapi-plugin-testing

v0.3.0

Published

Out-of-the-box automated testing for Strapi

Downloads

129

Readme

Testing plugin for Strapi (Beta)

This community plugin automates all the steps in the unit test page of the documentation by creating all the default files needed.

Compatible with Strapi ^3.5.4

Installation

  1. Install dependecy:
yarn add strapi-plugin-testing
  1. Run the project to generate all the files needed(check if __tests__ folder has been created after running)
yarn develop
  1. Add test command in package.json:
"test": "jest --forceExit --detectOpenHandles",
  1. Run the tests
yarn test

How to use

Jest will execute all the files inside of /__tests__/ that end in .test.js. Every .test.js file will be it's own instance of Strapi with it's own database, wich can add a lot of loading time (~10 seconds) before the tests start running.

To avoid re-creating a new Strapi instance for every test, .js files can be manually added to a .test.js file with require(...). In this case they will share the same instance and database.

Warning: If multiple tests share the same instance, the order at which the tests run can have an impact on the result.

File Structure

/__tests__/
  index.test.js

/config/
  /env/
    /test/
      database.js // database config used in tests

jest.config.js

Helper Functions

startStrapi

async startStrapi([callback])

Starts a new Strapi instance and returns it. Build with singleton pattern so it can be called multiple times without re-creating a new strapi instance.

Will pass the new Strapi instance as a parameter to the callback.

Check startStrapi's code for more details

startStrapiJest

async startStrapiJest([callback])

Starts a new Strapi instance before every test (with singleton pattern) and deletes the database and the end of every test. The Strapi instance will be globally available under the variable name strapi. The optional callback will be called at the end with the new Strapi instance.

Check startStrapiJest's code for more details

Examples:


function populateDatabase(strapiInstance) {
  await strapiInstance.services.restaurant.create({
    name: "Pizza",
  });
}

// creates instance and populates data
startStrapiJest(async (strapiInstance) => {
  await mockApplicationData(strapiInstance); // optional parameter
});

describe("Global setup", () => {
  it("strapi is defined", async (done) => {
    expect(strapi).toBeDefined(); // strapi globally available
    done();
  });
});

createStrapiSuperAdmin

async createStrapiSuperAdmin(strapiInstance [, email, password, username, firstname, lastname, blocked, isActive, displayLogs])

Creates a new Strapi Super Admin. A super admin is the user which can connect to the Strapi back-office, not the one the users table. Check the code for the default values.

Check createStrapiSuperAdmin's code for more details

Examples:

const {
  startStrapiJest,
  createStrapiSuperAdmin,
} = require("strapi-plugin-testing");

startStrapiJest();

describe("Global setup", () => {
  it("strapi is defined", async (done) => {
    await createStrapiSuperAdmin(strapi);
    await createStrapiSuperAdmin(strapi, "[email protected]", "password");
    expect(strapi).toBeDefined();
    done();
  });
});

activatePrivileges

async activatePrivileges(strapiInstance, roleId [, [...actions]])

Sets the privileges of roleId to true. If third argument omitted; sets all actions in roleId to true. Check the code for the default values.

Check activatePrivileges's code for more details

Examples:

const {
  activatePrivileges,
  getAuthenticatedRole,
} = require("strapi-plugin-testing");

const authenticatedRole = await getAuthenticatedRole(strapiInstance);

await activatePrivileges(strapiInstance, "order", authenticatedRole.id);
await activatePrivileges(
  strapiInstance,
  "product-category",
  authenticatedRole.id,
  ["count"]
);
await activatePrivileges(strapiInstance, "product", authenticatedRole.id, [
  "count",
  "delete",
  "find",
]);

getAuthenticatedRole

async getAuthenticatedRole(strapiInstance)

Check getAuthenticatedRole's code for more details

Examples:

const { getAuthenticatedRole } = require("strapi-plugin-testing");

const authenticatedRole = await getAuthenticatedRole(strapiInstance);
console.log("authenticatedRole.id", authenticatedRole.id);

getPublicRole

async getPublicRole(strapiInstance)

Check getPublicRole's code for more details

Examples:

const { getPublicRole } = require("strapi-plugin-testing");

const publicRole = await getPublicRole(strapiInstance);
console.log("publicRole.id", publicRole.id);

Other Use Cases

Test Watch

Test watch so any time a file changes, tests will re-run

    "testwatch": "jest --watchAll --forceExit --detectOpenHandles",

Bootstrap your project with dummy data

Use config/functions/bootstrap.js or run the mock functions in development environment so all the mocked data stays in the database.

const {
  setupStrapi,
  createStrapiSuperAdmin,
  activatePrivileges,
  getPublicRole,
  getAuthenticatedRole,
} = require("strapi-plugin-testing");

const populatteDatabase = async () => {
  const strapiInstance = await setupStrapi();

  await createStrapiSuperAdmin(strapiInstance, "[email protected]", "password");

  const authenticatedRole = await getAuthenticatedRole(strapiInstance);
  const publicRole = await getPublicRole(strapiInstance);

  await activatePrivileges(strapiInstance, "products", authenticatedRole.id); // full privilege
  await activatePrivileges(strapiInstance, "products", publicRole.id, [
    "count",
    "find",
    "findOne",
  ]);
  // ...
};

Trouble shooting

  • Delete .tmp folder and try again. If that fixes the issue maybe the database is not being deleted properly or is not running the proper database configuration.

  • May not work properly in Windows machines (check offitial doc for more information)

  • Feel free to create an issue in this repo

Work in progress

The plugin is in Beta, any feedback is welcome.

Possible functionalities for this plugin:

  • [ ] Automatically creat test files for the each api endpoint

  • [ ] Test coverage configured for Strapi so only the custom code is accounted for

  • [ ] Pre-commit test run

  • [ ] Easy setup for Github Actions