Strapi's test suite is written using mocha and although Strapi doesn't impose any testing framework for your apps, in this example we will setup tests using the mocha framework.


Before writing tests, you should setup a basic directory structure, like this:

├── api/
├── ...
├── test/
│  ├── integration/
│  │  ├── controllers/
│  │  │  └── my_endpoint.test.js
│  │  ├── models/
│  │  │  └── my_model.test.js
│  │  └── ...
|  ├── ...
│  ├── bootstrap.js


We are going to setup a bootstrap.js with before and after hooks to perform any actions before and after our tests.
In this example, the app server is started before running any tests an stop the server after tests are completed.


const strapi = require('strapi');

before(function (done) {
  strapi.start({}, function(err) {
    if (err) {
      return done(err);

    done(err, strapi);

after(function (done) {

Writing tests

Once you have setup your directory structure, you can start writing your tests. In this example we use co-supertest, a co and Supertest integration library. Supertest provides several useful methods for testing HTTP requests.
If you want to test an api endpoint, you can do it like this:


const request = require('co-supertest');

describe('MyEndpoint Controller Integration', function() {
  describe('GET /my_endpoint', function() {
    it('should return 200 status code', function *() {
      yield request(strapi.config.url)
        .expect('Content-Type', /json/)

Running tests

In order to run tests you can use npm test. In your package.json, in the scripts section, add this:


"scripts": {
  "test": "mocha --require co-mocha test/bootstrap.js test/**/*.test.js"

Remember to run test/bootstrap.js before any other tests and, if you want, use the --require option to pass any required dependencies you need available in your tests.