# Unit testing

In this guide we will see how you can run basic unit tests for a Strapi application using a testing framework.

πŸ’‘ TIP

In this example we will use Jest (opens new window) Testing Framework with a focus on simplicity and Supertest (opens new window) Super-agent driven library for testing node.js HTTP servers using a fluent API

βœ‹ CAUTION

Please note that this guide will not work if you are on Windows using the SQLite database due to how windows locks the SQLite file.

# Install test tools

Jest contains a set of guidelines or rules used for creating and designing test cases - a combination of practices and tools that are designed to help testers test more efficiently.

Supertest allows you to test all the api routes as they were instances of http.Server (opens new window)

sqlite3 is used to create an on-disk database that is created and deleted between tests.

Once this is done add this to package.json file

add test command to scripts section

  "scripts": {
    "develop": "strapi develop",
    "start": "strapi start",
    "build": "strapi build",
    "strapi": "strapi",
    "test": "jest --forceExit --detectOpenHandles"
  },
1
2
3
4
5
6
7

and add those line at the bottom of file

  "jest": {
    "testPathIgnorePatterns": [
      "/node_modules/",
      ".tmp",
      ".cache"
    ],
    "testEnvironment": "node"
  }
1
2
3
4
5
6
7
8

Those will inform Jest not to look for test inside the folder where it shouldn't.

# Introduction

# Testing environment

Test framework must have a clean empty environment to perform valid test and also not to interfere with current database.

Once jest is running it uses the test environment (switching NODE_ENV to test) so we need to create a special environment setting for this purpose. Create a new config for test env ./config/env/test/database.js and add the following value "filename": ".tmp/test.db"β€Š-β€Šthe reason of that is that we want to have a separate sqlite database for tests, so our test will not touch real data. This file will be temporary, each time test is finished, we will remove that file that every time tests are run on the clean database. The whole file will look like this:

Path β€” ./config/env/test/database.js

module.exports = ({ env }) => ({
  connection: {
    client: 'sqlite',
    connection: {
      filename: env('DATABASE_FILENAME', '.tmp/test.db'),
    },
    useNullAsDefault: true,
    debug: false
  },
});
1
2
3
4
5
6
7
8
9
10

# Strapi instance

In order to test anything we need to have a strapi instance that runs in the testing environment, basically we want to get instance of strapi app as object, similar like creating an instance for process manager.

These tasks require adding some files - let's create a folder tests where all the tests will be put and inside it, next to folder helpers where main Strapi helper will be in file strapi.js.

Path β€” ./tests/helpers/strapi.js

const Strapi = require("@strapi/strapi");
const fs = require("fs");

let instance;

async function setupStrapi() {
  if (!instance) {
    await Strapi().load();
    instance = strapi;
    
    await instance.server.mount();
  }
  return instance;
}

async function cleanupStrapi() {
  const dbSettings = strapi.config.get("database.connection");

  //close server to release the db-file
  await strapi.server.httpServer.close();

  // close the connection to the database before deletion
  await strapi.db.connection.destroy();

  //delete test database after all tests have completed
  if (dbSettings && dbSettings.connection && dbSettings.connection.filename) {
    const tmpDbFile = dbSettings.connection.filename;
    if (fs.existsSync(tmpDbFile)) {
      fs.unlinkSync(tmpDbFile);
    }
  }
}

module.exports = { setupStrapi, cleanupStrapi };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

# Test strapi instance

We need a main entry file for our tests, one that will also test our helper file.

Path β€” ./tests/app.test.js

const fs = require('fs');
const { setupStrapi, cleanupStrapi } = require("./helpers/strapi");

beforeAll(async () => {
  await setupStrapi();
});

afterAll(async () => {
  await cleanupStrapi();
});

it("strapi is defined", () => {
  expect(strapi).toBeDefined();
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Actually this is all we need for writing unit tests. Just run yarn test and see a result of your first test

yarn run v1.13.0
$ jest
 PASS  tests/app.test.js
  βœ“ strapi is defined (2 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        4.187 s
Ran all test suites.
✨  Done in 5.73s.
1
2
3
4
5
6
7
8
9
10
11

πŸ’‘ TIP

If you receive a timeout error for Jest, please add the following line right before the beforeAll method in the app.test.js file: jest.setTimeout(15000) and adjust the milliseconds value as you need.

# Testing basic endpoint controller.

πŸ’‘ TIP

In the example we'll use and example Hello world /hello endpoint from controllers section.

Some might say that API tests are not unit but limited integration tests, regardless of nomenclature, let's continue with testing first endpoint.

We'll test if our endpoint works properly and route /hello does return Hello World

Let's create a separate test file where supertest will be used to check if endpoint works as expected.

Path β€” ./tests/hello/index.js

const request = require('supertest');

it("should return hello world", async () => {
  await request(strapi.server.httpServer)
    .get("/api/hello")
    .expect(200) // Expect response http code 200
    .then((data) => {
      expect(data.text).toBe("Hello World!"); // expect the response text
    });
});

1
2
3
4
5
6
7
8
9
10
11

Then include this code to ./tests/app.test.js at the bottom of that file

require('./hello');
1

and run yarn test which should return

➜  my-project yarn test
yarn run v1.13.0
$ jest --detectOpenHandles
 PASS  tests/app.test.js (5.742 s)
  βœ“ strapi is defined (4 ms)
  βœ“ should return hello world (208 ms)

[2020-05-22T14:37:38.018Z] debug GET /hello (58 ms) 200
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        6.635 s, estimated 7 s
Ran all test suites.
✨  Done in 9.09s.
1
2
3
4
5
6
7
8
9
10
11
12
13
14

πŸ’‘ TIP

If you receive an error Jest has detected the following 1 open handles potentially keeping Jest from exiting check jest version as 26.6.3 works without an issue.

# Testing auth endpoint controller.

In this scenario we'll test authentication login endpoint with two tests

  1. Test /auth/local that should login user and return jwt token
  2. Test /users/me that should return users data based on Authorization header

Path β€” ./tests/user/index.js

const request = require('supertest');

// user mock data
const mockUserData = {
  username: "tester",
  email: "tester@strapi.com",
  provider: "local",
  password: "1234abc",
  confirmed: true,
  blocked: null,
};

it("should login user and return jwt token", async () => {
  /** Creates a new user and save it to the database */
  await strapi.plugins["users-permissions"].services.user.add({
    ...mockUserData,
  });

  await request(strapi.server.httpServer) // app server is an instance of Class: http.Server
    .post("/api/auth/local")
    .set("accept", "application/json")
    .set("Content-Type", "application/json")
    .send({
      identifier: mockUserData.email,
      password: mockUserData.password,
    })
    .expect("Content-Type", /json/)
    .expect(200)
    .then((data) => {
      expect(data.body.jwt).toBeDefined();
    });
});

it('should return users data for authenticated user', async () => {
  /** Gets the default user role */
  const defaultRole = await strapi.query('plugin::users-permissions.role').findOne({}, []);

  const role = defaultRole ? defaultRole.id : null;

  /** Creates a new user an push to database */
  const user = await strapi.plugins['users-permissions'].services.user.add({
    ...mockUserData,
    username: 'tester2',
    email: 'tester2@strapi.com',
    role,
  });

  const jwt = strapi.plugins['users-permissions'].services.jwt.issue({
    id: user.id,
  });

  await request(strapi.server.httpServer) // app server is an instance of Class: http.Server
    .get('/api/users/me')
    .set('accept', 'application/json')
    .set('Content-Type', 'application/json')
    .set('Authorization', 'Bearer ' + jwt)
    .expect('Content-Type', /json/)
    .expect(200)
    .then(data => {
      expect(data.body).toBeDefined();
      expect(data.body.id).toBe(user.id);
      expect(data.body.username).toBe(user.username);
      expect(data.body.email).toBe(user.email);
    });
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

Then include this code to ./tests/app.test.js at the bottom of that file

require('./user');
1

All the tests above should return an console output like

➜  my-project git:(master) yarn test

yarn run v1.13.0
$ jest --forceExit --detectOpenHandles
[2020-05-27T08:30:30.811Z] debug GET /hello (10 ms) 200
[2020-05-27T08:30:31.864Z] debug POST /auth/local (891 ms) 200
 PASS  tests/app.test.js (6.811 s)
  βœ“ strapi is defined (3 ms)
  βœ“ should return hello world (54 ms)
  βœ“ should login user and return jwt token (1049 ms)
  βœ“ should return users data for authenticated user (163 ms)

Test Suites: 1 passed, 1 total
Tests:       4 passed, 4 total
Snapshots:   0 total
Time:        6.874 s, estimated 9 s
Ran all test suites.
✨  Done in 8.40s.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18