Search code examples
node.jstypescriptjestjstimeout

Jest with Typescript error: Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Timeout


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;


Solution

  • 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