1

I'm trying to unit test a node.js controller that takes data from my mongodb database (using mongoose). In particular, before each test, I would like to clear the database and repopulate it with some sample data (the wordsCollection variable). Unfortunately, I'm getting an error relating to my done() function.

I thought this might be similar to this question, but my done() is within the beforeEach and it blocks, so not sure why it is not working.

//test file
'use strict';

var mongoose = require('mongoose'), 
  should = require('should'),
  config = require('../../../lib/config/config');

require('../../../lib/models/word');

var Word = mongoose.model('Word'),
  Words = require('../../../lib/controllers/words');

mongoose.connect(config.mongo.uri, config.mongo.options);

var meaningsChecker;
var wordsCollection = [
  //some data
];

describe('MeaningsChecker', function(done) {
  //I'm trying to clear the database before re-adding it
  beforeEach(function() {
    Word.find({}).remove(function() {
      Word.create(wordsCollection, function() {
        console.log('words stored')
        done();
      });
    });
  });

  it('should return a promise', function(done) {
    Word.find({}, function(err, words) {
      console.log(words);
      done(); //this line seems to be what is causing the error
    });
  });

});


//error message
Uncaught TypeError: undefined is not a function
  at Promise.<anonymous> (/Users/dantang/Desktop/production/jiejieandfriends/test/server/words/words.js:88:9)
  at Promise.<anonymous> (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
  at Promise.EventEmitter.emit (events.js:106:17)
  at Promise.emit (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
  at Promise.fulfill (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
  at Promise.<anonymous> (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/lib/model.js:1520:35)
  at Promise.<anonymous> (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
  at Promise.EventEmitter.emit (events.js:98:17)
  at Promise.emit (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
  at Promise.fulfill (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
  at handleSave (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/lib/model.js:133:13)
  at /Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/lib/utils.js:408:16
  at /Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mongodb/lib/mongodb/collection/core.js:216:9
  at Server.Base._callHandler (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:442:41)
  at /Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:485:18
  at MongoReply.parseBody (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
  at null.<anonymous> (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:443:20)
  at EventEmitter.emit (events.js:95:17)
  at null.<anonymous> (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:191:13)
  at EventEmitter.emit (events.js:98:17)
  at Socket.<anonymous> (/Users/dantang/Desktop/production/jiejieandfriends/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:418:22)
  at Socket.EventEmitter.emit (events.js:95:17)
  at Socket.<anonymous> (_stream_readable.js:746:14)
  at Socket.EventEmitter.emit (events.js:92:17)
  at emitReadable_ (_stream_readable.js:408:10)
  at emitReadable (_stream_readable.js:404:5)
  at readableAddChunk (_stream_readable.js:165:9)
  at Socket.Readable.push (_stream_readable.js:127:10)
  at TCP.onread (net.js:528:21)
Community
  • 1
  • 1
Dan Tang
  • 1,273
  • 2
  • 20
  • 35
  • at what point in the code does this error trigger? For fun, replace those `done();` with `if (done) { done(); } else { console.error("omg done() was never passed into my test runner"); }` – Mike 'Pomax' Kamermans Sep 26 '14 at 23:00

0 Answers0