Unable to load Strapi in jest environment

System Information
  • Strapi Version: 3.1.5
  • Operating System: macOS Big Sur 11.0.1
  • Database: postgres
  • Node Version: v14.15.1
  • NPM Version: 6.14.9

Hey,

I am following this guide provided in the Strapi docs and have been unsuccessful in testing strapi.

I have created the following files:

/* ./config/env/test/database.json */

{
  "defaultConnection": "default",
  "connections": {
    "default": {
      "connector": "bookshelf",
      "settings": {
        "database": "${process.env.DATABASE_NAME || 'strapi_development'}"
      },
      "options": {
        "useNullAsDefault": true,
        "pool": {
          "min": 0,
          "max": 1
        }
      }
    }
  }
}
/* ./tests/helpers/strapi.js */

const Strapi = require('strapi');
const http = require('http');

let instance;

async function setupStrapi() {
  if (!instance) {
    /** the following code in copied from `./node_modules/strapi/lib/Strapi.js` */
    try {
      await Strapi().load();
    } catch (error) {
      console.log(error);
    }

    instance = strapi; // strapi is global now

    try {
      await instance.app
        .use(instance.router.routes()) // populate KOA routes
        .use(instance.router.allowedMethods()); // populate KOA methods
    } catch (error) {
      console.log(error);
    }

    instance.server = http.createServer(instance.app.callback());
  }
  return instance;
}
module.exports = { setupStrapi };
/* ./tests/app.test.js */

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

jest.setTimeout(15000);

/** this code is called once before any test is called */
beforeAll(async done => {
  await setupStrapi(); // singleton so it can be called many times
  done();
});

/** this code is called once before all the tested are finished */
afterAll(async done => {
  const dbSettings = strapi.config.get('database.connections.default.settings');

  //delete test database after all tests
  if (dbSettings && dbSettings.filename) {
    const tmpDbFile = `${__dirname}/../${dbSettings.filename}`;
    if (fs.existsSync(tmpDbFile)) {
      fs.unlinkSync(tmpDbFile);
    }
  }
  done();
});

it('strapi is defined', () => {
  expect(strapi).toBeDefined();
});

When running jest --forceExit --detectOpenHandles I am getting the error

 ● strapi is defined

    Timeout - Async callback was not invoked within the 15000 ms timeout specified by jest.setTimeout.Error: Timeout - Async callback was not invoked within the 15000 ms timeout specified by jest.setTimeout.

      at mapper (node_modules/jest-jasmine2/build/queueRunner.js:27:45)

It looks like the error is originating from strapi.load();

The app does run locally and in production without any problems, the issue is isolated to the test environment.

1 Like

Your missing some options in that database.js file. This is an example from our documentation for SQLite:

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'sqlite',
        filename: env('DATABASE_FILENAME', '.tmp/data.db'),
      },
      options: {
        useNullAsDefault: true,
      },
    },
  },
});

@DMehaffy he does not miss any option !
You should focus that he has two database files : [ database.json , database.js ] !!
When following the unit test documentation , it demand create a new database.json file !
@ercgnclvs I am facing the same error right now !

I completely missed that, your right, you shouldn’t have a database.json file only the .js one