mex111
mex111

Reputation: 77

Mocha test.Timeout 2000ms

What I am doing wrong?I am trying to save phones to my db,but mocha says Timeout of 2000ms exceeded. My phone schema

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const phoneSchema = new Schema({
    company: String,
    modelname: String,
    numbername: String,
    picture: String,
    price: String,
});

const Phone = mongoose.model('phone', phoneSchema);

module.exports = Phone;

and my test file:

    const mocha = require('mocha');
const assert = require('assert');
const Phone = require('../models/phone-model');


//describe tests
describe('Saving records', function(){
    //create tests
    it('Saves phone to the db',function(done){

        var phone1 = new Phone({
            company: "Samsung",
            modelname: "Galaxy",
            numbername: "S5",
            picture: "https://drop.ndtv.com/TECH/product_database/images/2252014124325AM_635_samsung_galaxy_s5.jpeg",
            price: "12500P"
        });

        phone1.save().then(function(){
            assert(phone1.isNew === false);
            done(); 
        });

    });

});

I don't understand what I am doing wrong here..it always works for me before this. Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\User\Desktop\Oauth\test\phone_test.js)

Upvotes: 0

Views: 376

Answers (1)

sripberger
sripberger

Reputation: 1732

As comments have said, you're only calling done if the promise from the save method resolves, meaning that your test will time out if your database sends an error response.

Since you're dealing with promises, try using mocha's built-in promise support instead of the done callback:

const mocha = require('mocha');
const assert = require('assert');
const Phone = require('../models/phone-model');

describe('Saving records', function(){
    // Note there is no `done` argument here
    it('Saves phone to the db',function(){

        var phone1 = new Phone({
            company: "Samsung",
            modelname: "Galaxy",
            numbername: "S5",
            picture: "https://drop.ndtv.com/TECH/product_database/images/2252014124325AM_635_samsung_galaxy_s5.jpeg",
            price: "12500P"
        });

        // Note returning the promise here...
        return phone1.save().then(function(){
            assert(phone1.isNew === false);
        });

    });

});

Let us know what you see after this. Hopefully you'll see an actual error message that will help you track down the problem. Your DB also very well could be taking longer than 2000ms to respond, though this is more likely.

Upvotes: 1

Related Questions