Strapi testing with Jest. More examples #1.

Mateusz Wojczal
Escolasoft
Published in
2 min readOct 15, 2020

In this article I’ll try to focus on pure examples of testing strategies in Strapi 3. It’s a continuation of previous medium post

All the examples are covered in github repository and all the tests are run by CircleCi.

Supporting tools

For a more efficient (DRY) workflow I’m going to use the same straight-forward factories for mock objects and helpers to interact with strapi instance.

Factories

User factory

Extended helpers

Strapi general helper was introduced in a previous article, yet this one is a bit extended.

Strapi helper

JSON Web Token signed request

To get a Bearer token required for all authenticated requests you can either

- call /auth/local endpoint or
- use strapi.plugins[“users-permissions”].services.jwt.issue method.

Strapi helper from above contains method jwt to perform fetching the token

Allowing roles for test routes

A custom route and controller that is not public (needs JWT token in Request header) must be assigned to role — otherwise even for a valid token controller will throw Forbidden 403 error. The whole process is described in Authenticated request tutorial on Strapi documentation page. This information is saved in the database only.

During a tests we need to be able to mimic the administrator panel. Helper grantPrivilage allows that.

Example

Lets create an endpoint that for an authenticated user returns a text Hi ${ctx.state.user.username}.

./api/hello/config/routes.json

./api/hello/controllers/Hello.js

Yet the test

will not work, because /hi is not assigned to a role that user is. The following code fix this

Testing sending emails

For testing if emails are sent and their content one need to create new providera mock one.

nodemailer-mock allows us to mock the nodemailer and test what we need. Add module to dependencies with
yarn add nodemailer-mock and create a new config for test environment.

./config/env/test/plugins.js

Examples (test files)

Below are first part of examples.

Custom endpoint

User tests

If the following file test checks if

  • user can register
  • user can login (jwt tokens are fine)
  • user can confirm his account with received email with confirmation link
  • user can’t login if account is not confirmed

performs sample e2e test, in the following sequence:

  1. Sends a request to register new user
  2. Tests if user exists in database now and it is not confirmed
  3. User should not be able to login at this stage
  4. Email is being sent
  5. Fetch and test confirmation link from email content
  6. Calling a email-confirmation should confirm user and return a 302 redirect
  7. Check in database if user in confirmed now, link from email was clicked
  8. Finally user can login in

Next file test if

- user can reset password with received email with reset link

  1. Send email to forgot password
  2. The code with new password on /auth/reset-password
  3. Change password
  4. Test if password has changed

This series of articles are going to be continued, so please stay tuned.

--

--

Mateusz Wojczal
Escolasoft

founder of Qunabu Interactive from Gdańsk, Poland. Full-stack web developer with over a dozen years of experience.