2
votes

I'm studying how to create some tests using the Jest with Nodejs, i'm actually using typescript. When I try to run a simple test, by checking the status of the response, it shows the following error:

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

What could I do?

Here's my following codes:

session.test.ts =>

const request = require('supertest');
import app from '../../src/server';
describe('Authentication',() => {
    it('should authenticate with valid credentials',async() =>{
        const response = await request(app)
        .post('/sessions')
        .send({
            email: "[email protected]",
            password: "123456"
        })

        await expect(response.status).toBe(200);
    });

});

SessionController.ts =>

import {Request, Response} from 'express';
export default class SessionController{

async store(request: Request, response: Response){
        return response.status(200);
    }
    
} 

server.ts =>

import express from 'express'; 
import routes from './routes';
require("dotenv").config({
    path: process.env.NODE_ENV === "test" ? ".env.test" : ".env"
  });
  
const app = express();

app.use(express.json());
app.use(routes);
app.listen(3333);

export default app;

and routes.ts:

import express from 'express';
import UsersController from './controllers/UsersController';
import SessionController from './controllers/SessionController';

const routes = express.Router();
const usersControllers = new UsersController();
const sessionController = new SessionController();

routes.post('/users',usersControllers.create);

routes.post('/sessions',sessionController.store);


export default routes;

3

3 Answers

1
votes

at my SessionController.ts, I had to put the following:

import {Request, Response} from 'express';
export default class SessionController{

async store(request: Request, response: Response){
        return response.status(200).send()
    }
    
} 

I forgot to send haha

0
votes

The first thing is to check if there are some error in the request, or (more likely) if it remain in a pending state, because 5 seconds are tons of time.

Anyway you can specify the test timeout like follow

describe('Authentication',() => {
  it('foobar', async function () { // no arrow function
    this.timeout(10000)
    await myFunc()
  });
});
0
votes

I am not sure you are actually 'completing' the request using the supertest API.

The fluent chaining approach of supertest allows you to carry on adding 'steps' to the HTTP request before actually dispatching it. Unfortunately, send() is preparing a send step for when you dispatch. It doesn't actually dispatch as you can see from this superagent example in which many further configuration steps follow send() and it's only end() which runs them all.

In several supertest examples I saw there is a chained 'expect' call which would presumably also trigger the actual HTTP post.

Equally, the 'end()' docs at https://github.com/visionmedia/supertest#endfn say...

Perform the request and invoke fn(err, res)

This indicates to me that until you send a 'finalising' call, there won't be a request.