Escolasoft
Published in

Escolasoft

Strapi testing with Jest. More examples #1.

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.

--

--

--

Stories from creative software house based in Poland, with a love for all web technology

Recommended from Medium

The SkaeHub Boot Camp Experience

Solving Complex Problems with Systems Thinking

Bitcoin Trade Automation with Awesome Oscillator in Python

Learn Python List Comprehension

Create a Custom Configuration Provider in ASP.NET Core

Installing Go for first-time programmers on a Mac

SRE vs Platform Engineering

Installing RustUp on MacOSX and Fish Shell

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mateusz Wojczal

Mateusz Wojczal

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

More from Medium

How to Publish React App on Github By Using Yarn ?

Caching API responses in React

Setting up GoogleMaps API on React.js

What is Broken authentication and How to Prevent it in React Application