3
votes

I am testing API for User functionality (signup and login) using jest.

Code for testing:

const request = require('supertest');
const app = require('../../app');

describe('Test User Functionality', () => {  
  test('User should be able to login', async done => {
    const response = await request(app)
      .post('/api/users/login')
      .send({
        email: '[email protected]',
        password: 'welcome1',
      })
      .expect(200);
    done();
    //expect(response.statusCode).toBe(200);
  });
  test('User should be able to signup', async done => {
    const response = await request(app)
      .post('/api/users/signup')
      .send({
        username: 'testuser',
        email: '[email protected]',
        password: 'welcome1',
      })
      .expect(200);
    done();
    //expect(response.statusCode).toBe(200);
  });
});

In case I have single test, it is working fine but with multiple test inside describe, it is showing timeout errors.

Below is the screenshot of the error:

enter image description here

I tried adding timeout, swapping the test but still no success.

Anyone please help !

2
Did you ever solve this?nkhil

2 Answers

0
votes

I've encountered same issue when testing my react component with async requests.

This happens because you didn't finished your request properly.

You can easily solve this.

Option 1: move the done as the second parameter for expect function call like below.

const response = await request(app)
  .post('/api/users/signup')
  .send({
    username: 'testuser',
    email: '[email protected]',
    password: 'welcome1',
  })
  .expect(200, done);

Option2: use end method

const response = await request(app)
      .post('/api/users/signup')
      .send({
        username: 'testuser',
        email: '[email protected]',
        password: 'welcome1',
      })
      .expect(200)
      .end((err, res) => { 
        // do anything you want! 
      })

Or you can checkout the document(https://github.com/visionmedia/supertest#readme)!

0
votes

One possible issue might be with express middlewares. To find out if this is your case, you could:

  1. Comment out all the middlewares for your express app (app.use(/* middleware */) docs)
  2. See if the tests start progressing somehow (pass/not time out)
  3. Start uncommenting the middlewares, to narrow it down to the culprit.

Once you've found where the cause of timeout comes from, you can go deeper. Mock different parts of the middleware that is causing the issue:

  1. Mock third party libraries by creating a directory __mocks__ in your root directory and insert a file library_name.js (this is manual mocking for jest: documentation)
  2. Mock middlewares and pass right through them by making them just call next app.use(Your.Middleware) and in your middleware file
/** Possibly imports and some setup here */

// Make sure the function being called is mocked
export default {
    Middleware: (req, res, next) => {
        next();
    },
}

(File containing your middleware might contain some additional setup which might cause the issue.)

My particular case: I was using ioredis in middlewares and in the file on the Redis instantiation it tried to connect to the store multiple times, which caused the time out.
As it turns out the hostname was wrong. The key which gave the issue away was mocking middlewares and finding the extra setup. Then Jest showed another error hinting at store connection problems.