V4 Jest Testing

System Information
  • Strapi Version: 4,1,1
  • Operating System: Linux Ubuntu 20.04
  • Database: sqlite
  • Node Version: 12.8.0
  • NPM Version: 6.14.7
  • Yarn Version:

When running a GET request against a simple ‘hello world’ api, I always get a Timeout. The request returns with 200 and the content is as expected. But the request throws a timeout exception after 15 sec.

const request = require('supertest');
const strapi = require("@strapi/strapi");
const fs = require("fs");
const http = require('http');

let instance;
let server;

jest.setTimeout(15000);

beforeAll(async () => {
  if (!instance) {
    instance = await strapi().load(); 
    instance.server.mount()
    server = instance.server.app.callback()
  }
});

afterAll(async () => {
  const dbSettings = instance.config.get('database.connections.default.settings');
  
  await instance.destroy();

  if (dbSettings && dbSettings.filename) {
    const tmpDbFile = `${__dirname}/../${dbSettings.filename}`;
    if (fs.existsSync(tmpDbFile)) {
      fs.unlinkSync(tmpDbFile);
    }
  }
});

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

it('should return hello world', async (done) => {
  const response = await request(server).get('/api/hello')
  expect(response.statusCode).toBe(200)
  expect(response.text).toBe('Hello World!'); // expect the response text
});

stdout:

.nvm/versions/node/v14.8.0/bin/node /workspace/toastmasters-contest-managing-tool/backend/node_modules/.bin/jest --runInBand 
Debugger attached.
[2022-02-28 22:37:01.391] http: GET /api/hello (19 ms) 200
 FAIL  tests/app.test.js (67.204 s)
  ● should return hello world

    thrown: "Exceeded timeout of 15000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

      34 | });
      35 |
    > 36 | it('should return hello world', async (done) => {
         | ^
      37 |   const response = await request(server).get('/api/hello')
      38 |   expect(response.statusCode).toBe(200)
      39 |   expect(response.text).toBe('Hello World!'); // expect the response text

      at Object.<anonymous> (tests/app.test.js:36:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

after hours of try and error I really run out of ideas.

I’d really appreciate to hear if someone gets a jest unit test running.

I followed the instructions according Unit Testing - Strapi Developer Docs with no luck. See above.

Has anybody experience or a helpful hint?

'should return hello world'

is a string? Surely that has to be matched with the response somehow? If so how? Is the string passed as an argument in the “it” function? (ive never used jest before so just going off what i can see)