Timeout using mongoose createConnection for mocha test

I have a problem with the mongoose.createConnection function , here is my code for the tests:

"use strict";
// connect to mongodb://localhost/node_marque_test
// empty database before each test

let mongoose = require('mongoose'),
    expect = require('chai').expect,
    // use a specific base for test purposes
    dbURI = 'mongodb://localhost/node_marque_test',
    Marque = require('../lib/marque.js');

before(function(done){
  // connect to db
  let connection = mongoose.createConnection(dbURI);
  // remove all documents
  connection.on('open', function(){

    Marque.remove(function(err, marques){
      if(err){
        console.log(err);
        throw(err);
      } else {
        // console.log('cleaning marques from mongo');
        done();
      }
    })
  })
})
afterEach(function(done){
  Marque.remove().exec(done);
})

describe('an instance of Marque', ()=>{
  let marque;
  beforeEach((done)=>{
    marque = new Marque({name: 'YAMAHA'})
    marque.save((err)=>{
      if(err){throw(err);}
      done();
    })
  })
  it('has a nom', ()=>{
    expect(marque.name).to.eql('YAMAHA');
  })

  it('has a _id attribute', ()=>{
     expect(marque).to.have.property('_id')
  })
})

And here is the Mark object code :

"use strict";
let mongoose = require('mongoose'), Schema = mongoose.Schema;

// Schema definition with some validation
let marqueSchema = Schema({
    name: { type: String, required: true}
});

// compile schema to create a model
let Marque = mongoose.model('Marque', marqueSchema);

// custom validation rules
Marque.schema.path('name').validate(name_is_unique, "This name is already taken");

function name_is_unique(name,callback) {
    Marque.find({$and: [{name: name},{_id: {$ne: this._id}}]}, function(err, names){
        callback(err || names.length === 0);
    });
}

module.exports = mongoose.model('Marque');

So when I run npm test , I got this error:

  1) "before all" hook

  0 passing (2s)
  1 failing

  1)  "before all" hook:
     Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

But if I replaced

// connect to db
let connection = mongoose.createConnection(dbURI);
// remove all documents
connection.on('open', function(){

Across

// connect to db
mongoose.connect(dbURI);
// remove all documents
mongoose.connection.on('open', function(){

Everything works and passes the test:

  an instance of Marque
    βœ“ has a nom
    βœ“ has a _id attribute


  2 passing (65ms)

The problem is that I need to do tests with multiple values, so I cannot use mongoose.connect (otherwise I received Error: attempt to open a closed connection. )

So how can I use createConnection to connect to mongoose inside my tests?

Thank you for your help:)

+4
1

, . connection.model mongoose.model.

, , , mongoose , .

, marque.js.

...
let connection = mongoose.createConnection(dbURI);
Marque = require('../lib/marque.js')(connection);
...

marque.js:

"use strict";
let mongoose = require('mongoose'), Schema = mongoose.Schema;

// Schema definition with some validation
let marqueSchema = Schema({
    name: { type: String, required: true}
});

module.exports = function(conn) {
    // compile schema to create a model. Probably should use a try-catch.
    let Marque = conn.model('Marque', marqueSchema);

    // custom model validation code here
    // ...

    return conn.model('Marque');
}
+1

Source: https://habr.com/ru/post/1611073/


All Articles