0

I can't figure out what's wrong with my code... I'm testing my API endpoint with the following test but it timeout.

When I test it with postman or curl it works perfectly it only timeout when it is tested with mocha and chai unit tests.

Test:

describe('test', () => {
    it('test', (done) => {
        chai.request(app)
            .get('/user')
            .end((err, res) => {
                res.should.have.status(200);
                done();
            })
    })
});

Endpoint:

const express = require('express');
const router = express.Router();
const database = require('../database/connection');

router.get('/', async (req, res) => {
    const collection = database.collection('user');
    const users = await collection.find().toArray();

    res.status(200).send(users);
});

module.exports = router;

connection.js

const config = require('../config');
const mongoose = require('mongoose');

mongoose.connect(
    'mongodb://' + config.database.username + ':' + config.database.password + '@' + config.database.host + ':' + config.database.port + '/' + config.database.database,
    { useNewUrlParser: true, useUnifiedTopology: true }
);

let database = mongoose.connection;

database.on('error', () => {
    console.error.bind(console, 'Failed to connect to mongo database');
    process.exit(1);
});
database.once('open', () => {
    if (config.name !== 'tests') {
        console.log('Connected to mongo database');
    }
});

module.exports = database;
hexa
  • 15
  • 2
  • Small precision, it timeout during ```await collection.find().toArray();``` – hexa Aug 29 '21 at 19:57
  • Does this answer your question? [How to increase timeout for a single test case in mocha](https://stackoverflow.com/questions/15971167/how-to-increase-timeout-for-a-single-test-case-in-mocha) – PLASMA chicken Aug 29 '21 at 20:16
  • @PLASMAchicken Unfortunately no I tried to increase the timeout to 30 seconds and it still timeout... it look like my endpoint can't retrieve data from the database which is strange because my API is correctly connected to the database and I insist it only occur when I test the endpoint with mocha and chai, manual test with postman or curl works. – hexa Aug 29 '21 at 20:21
  • could you add console log(date) in `router.get('/', async (req, res) => {` ? – mohammad Naimi Aug 29 '21 at 20:51
  • @mohammadNaimi yep I did it appears that the endpoint is correctly called but it's stuck at the line: ```await collection.find().toArray();``` – hexa Aug 29 '21 at 20:54
  • remove `.toArray()` and try it again – mohammad Naimi Aug 29 '21 at 20:57
  • @mohammadNaimi same thing when I remove ```toArray()``` :( – hexa Aug 29 '21 at 21:02
  • Whats your idea if we change await to promise ? – mohammad Naimi Aug 29 '21 at 21:30
  • Does it correctly Log "Connected to mongo database". Make sure your test config is working. – PLASMA chicken Aug 29 '21 at 22:54
  • @PLASMAchicken yes during the unit tests I'm correctly connected to the database an it log 'Connected to mongo database'. – hexa Aug 30 '21 at 08:13
  • @mohammadNaimi don't work too – hexa Aug 30 '21 at 08:16

0 Answers0