Testing with a Postgres database

You might also setup your tests like this:

./tests/helpers/close-database.js

const closeDatabase = async () => {
  const { filename } = strapi.config.get(
    'database.connections.default.settings'
  );
  if (filename) {
    const tmpDbFile = path.join(__dirname, '..', '..', filename);
    await strapi.db.destroy();
    if (fs.existsSync(tmpDbFile)) {
      fs.unlinkSync(tmpDbFile);
    }
  }
};

./tests/helpers/strapi-setup.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` */
    await Strapi().load();
    instance = strapi; // strapi is global now
    await instance.app
      .use(instance.router.routes()) // populate KOA routes
      .use(instance.router.allowedMethods()); // populate KOA methods

    instance.server = http.createServer(instance.app.callback());
  }
  return instance;
}

./tests/app.spec.js

const { setupStrapi, closeDatabase } = require('./helpers'); // you might also add jest.setTimeout(newTimeout) if needed in this file.

// 1. unit under test
describe('Global Strapi', () => {
  /** this code is called once before any test is called */
  beforeAll(async () => {
    await setupStrapi(); // singleton so it can be called many times
  });

  /** this code is called once before all the tested are finished */
  afterAll(async () => {
    await closeDatabase();
  });
  // 2. scenario and 3. expectation
  test('When setting up properly strapi should be defined', () => {
    expect(strapi).toBeDefined();
  });
});

This allows you to use sqlite for jest testing with windows. Problem was, strapi does not close the connections by default, so you have to close the strapi db connection before you unlink the .tmp/test.db file.

Hope it helps.