1

Summary of Problem Receiving : Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Timeout when trying to run a test with Jest and supertest.

Specs Koa2 project, Jest/Supertest testing, Babel 7.9.0 recommended configuration

What I've tried I have a simple test from the same file running which I omitted from the code below for brevity. I've also tried sending an HTTP request from the browser - this file is imported & 'listen'ed in a server file. The request is failing because it is blocked by a CORS policy - I think this is a problem for another day and isn't affecting my test timing out.

I also tried removed .callback() from the supertest(..) call:

const response = await supertest(app).post('/save-material');

  • at which point I get TypeError: app.dress is not a function.

Here is the content of my test file:

process.env.NODE_ENV = 'test';

const app = require('../../src/server/app.js')
const supertest = require('supertest')
 
test('save-material returns response', async() => {
    const response = await supertest(app.callback()).post('/save-material');
    expect(response.status).toBe(200);
    expect(response.body.status).toBe('success');
    expect(response.body.msg).toBe('Material saved')
});

Here is the content of the imported file (app.js) from above:

require('@babel/register'); // not entry point - but is entry point for some tests
const Koa = require('koa');
var Router = require('koa-router')
const app = new Koa();
const router = new Router();

router
    .post('/save-material', async(ctx) => {
        ctx.response = {
            status: 'success',
            msg: 'Material saved'
        }
        return ctx;
    })

app.use(router.routes());
app.use(router.allowedMethods());

module.exports = app;
Michael Jay
  • 503
  • 3
  • 15

0 Answers0