tjuba
tjuba

Reputation: 25

Ensure "done()" is called JS Error

Trying to insert new element into my mongoDB database, when I use terminal to 'npm run test' it gives me this error:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure 
"done()" is called; if returning a Promise, ensure it resolves.

Here is my code for saving an element into my DB:

const mocha = require('mocha');
const assert = require('assert');
const PPB = require('../Models/Pingpongballs');

describe('Saving records', function() {
  it('Saves record to db', function(done) {
    var ppball = new PPB ({
      amount: 5
    });

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

Upvotes: 0

Views: 428

Answers (1)

alexmac
alexmac

Reputation: 19617

In mocha, for async tests you could call done callback or return the promise. You're getting this error, because your test is failed, and you haven't catch block to call done with error:

describe('Saving records', function() {
  it('Saves record to db', function() {
    var ppball = new PPB ({
      amount: 5
    });

    return ppball.save().then(function(){
      assert(ppball.isNew === false);
      return null;
    });
  });
});

Upvotes: 3

Related Questions