testing - Mocha test fails when watching ("after all": done() called multiple times) -
i have mocha test passes fine on first run, when changes made , mocha re-runs test sec time (using --watch
flag), fails. test:
var mongoose = require('mongoose') , chai = require('chai') , request = require('supertest') , _ = require('underscore') , app = require('../../app').boot('testing') , link = mongoose.model('link') , should = chai.should() request = request(app) describe('links resource', function () { var userid = '512298b46271845564000002' , linkid , samplelink = { 'uri': 'http://1.com/', 'meta': { 'title': 'link 1', 'desc': 'lorem ipsum' }, 'shares': [{ 'uid': userid, 'date': new date(), 'message': 'lorem ipsum' }] } before(function (done) { mongoose.connection.on('open', done) }) after(function (done) { link.remove(function () { mongoose.connection.close(done) }) }) it('post /api/links', function (done) { request.post('/api/links') .send(samplelink) .expect('content-type', /json/) .expect(200) .end(function (err, res) { if (err) done(err) res.body.should.have.property('_id') linkid = res.body._id done() }) }) it('get /api/links/:id', function (done) { request.get('/api/links/' + linkid) .expect('content-type', /json/) .expect(200) .end(function (err, res) { if (err) done(err) res.body.should.have.property('uri', samplelink.uri) done() }) }) it('get /api/links', function (done) { request.get('/api/links') .expect('content-type', /json/) .expect(200) .end(function (err, res) { if (err) done(err) res.body.should.have.lengthof(1) done() }) }) it('patch /api/links/:id', function (done) { request.patch('/api/links/' + linkid) .send({ 'uri': 'http://2.com/' }) .expect('content-type', /json/) .expect(200) .end(function (err, res) { if (err) done(err) res.body.should.have.property('_id') res.body.should.have.property('uri', 'http://2.com/') done() }) }) it('put /api/links/:id', function (done) { request.put('/api/links/' + linkid) .send(_(samplelink).extend({ 'uri': 'http://3.com/' })) .expect('content-type', /json/) .expect(200) .end(function (err, res) { if (err) done(err) res.body.should.have.property('_id') res.body.should.have.property('uri', 'http://3.com/') done() }) }) it('delete /api/links', function (done) { request.del('/api/links/' + linkid) .expect(200) .end(function (err, res) { if (err) done(err) res.body.should.be.empty done() }) }) })
the error(s):
links resource 1) "after all" hook ✖ 1 of 6 tests failed: 1) links resource "after all" hook: error: done() called multiple times @ multiple (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:177:31) @ done (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:183:26) @ nativeconnection.runnable.run.duration (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:199:9) @ nativeconnection.eventemitter.emit (events.js:126:20) @ open (/users/oliver/development/personal/reader/node_modules/mongoose/lib/connection.js:431:10) @ nativeconnection.connection.onopen (/users/oliver/development/personal/reader/node_modules/mongoose/lib/connection.js:439:5) @ connection._open (/users/oliver/development/personal/reader/node_modules/mongoose/lib/connection.js:399:10) @ nativeconnection.doopen (/users/oliver/development/personal/reader/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:48:5) @ db.open (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:230:14) @ server.connect.connectcallback (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:333:7) @ g (events.js:192:14) @ eventemitter.emit (events.js:126:20) @ server.base._callhandler (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:130:25) @ server.connect.connectionpool.on.server._serverstate (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:517:20) @ mongoreply.parsebody (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:127:5) @ server.connect.connectionpool.on.server._serverstate (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:476:22) @ eventemitter.emit (events.js:96:17) @ _connect (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:175:13) @ eventemitter.emit (events.js:99:17) @ socket.exports.connection.createdatahandler (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:372:22) @ socket.eventemitter.emit (events.js:96:17) @ tcp.onread (net.js:397:14) 1) "before all" hook ✖ 1 of 6 tests failed: 1) links resource "before all" hook: error: done() called multiple times @ multiple (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:177:31) @ done (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:183:26) @ nativeconnection.runnable.run.duration (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:199:9) @ nativeconnection.eventemitter.emit (events.js:126:20) @ open (/users/oliver/development/personal/reader/node_modules/mongoose/lib/connection.js:431:10) @ nativeconnection.connection.onopen (/users/oliver/development/personal/reader/node_modules/mongoose/lib/connection.js:439:5) @ connection._open (/users/oliver/development/personal/reader/node_modules/mongoose/lib/connection.js:399:10) @ nativeconnection.doopen (/users/oliver/development/personal/reader/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:48:5) @ db.open (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:230:14) @ server.connect.connectcallback (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:333:7) @ g (events.js:192:14) @ eventemitter.emit (events.js:126:20) @ server.base._callhandler (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:130:25) @ server.connect.connectionpool.on.server._serverstate (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:517:20) @ mongoreply.parsebody (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:127:5) @ server.connect.connectionpool.on.server._serverstate (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:476:22) @ eventemitter.emit (events.js:96:17) @ _connect (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:175:13) @ eventemitter.emit (events.js:99:17) @ socket.exports.connection.createdatahandler (/users/oliver/development/personal/reader/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:372:22) @ socket.eventemitter.emit (events.js:96:17) @ tcp.onread (net.js:397:14)
i tried reproduce simple test cases, couldn't (mongoose 3.5.5)
this fixing effect, rather cause, maybe dropping collection straight work? illustration another answer:
after(function (done) { mongoose.connection.collections['links'].drop(); mongoose.connection.close(done); });
testing tdd mocha supertest superagent
No comments:
Post a Comment