Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with mo(ck/ng)oose and testing #175

Closed
Martinsos opened this issue May 22, 2018 · 3 comments
Closed

Problems with mo(ck/ng)oose and testing #175

Martinsos opened this issue May 22, 2018 · 3 comments

Comments

@Martinsos
Copy link

Hi,
thanks for this generator!
I have been using it for some time now, and I just encountered certain problems with testing and mockgoose.
The version of the generator is about 9 months old, I have mockgoose version 6.0.8 and mongoose 4.13.12.
I have not been using tests at all for some time and decided to start using them now. I updated all of them, they are all passing, but, I am getting a lot of errors from mongo, like this:

Ran all test suites matching /src\/services\/aws\/index.test.js/i.
  console.warn node_modules/bluebird/js/release/debuggability.js:873
    Unhandled rejection MongoError: topology was destroyed
        at createIndex (/home/XXX/api-server/node_modules/mongodb/lib/db.js:1071:76)
        at Db.Object.<anonymous>.Db.createIndex (/home/XXX/api-server/node_modules/mongodb/lib/db.js:1049:44)
        at /home/XXX/api-server/node_modules/mongodb/lib/db.js:1161:12
        at handleCallback (/home/XXX/api-server/node_modules/mongodb/lib/utils.js:120:56)
        at /home/XXX/api-server/node_modules/mongodb/lib/db.js:1586:5
        at handleCallback (/home/XXX/api-server/node_modules/mongodb/lib/utils.js:120:56)
        at /home/XXX/node_modules/mongodb/lib/cursor.js:861:16
        at handleCallback (/home/XXX/api-server/node_modules/mongodb-core/lib/cursor.js:171:5)
        at setCursorDeadAndNotified (/home/XXX/api-server/node_modules/mongodb-core/lib/cursor.js:505:3)
        at nextFunction (/home/XXX/api-server/node_modules/mongodb-core/lib/cursor.js:660:7)
        at CommandCursor.Object.<anonymous>.Cursor.next [as _next] (/home/XXX/api-server/node_modules/mongodb-core/lib/cursor.js:701:3)
        at fetchDocs (/home/XXX/api-server/node_modules/mongodb/lib/cursor.js:857:10)
        at /home/XXX/api-server/node_modules/mongodb/lib/cursor.js:880:7
        at handleCallback (/home/XXX/api-server/node_modules/mongodb-core/lib/cursor.js:171:5)
        at nextFunction (/home/XXX/api-server/node_modules/mongodb-core/lib/cursor.js:691:5)
        at /home/XXX/api-server/node_modules/mongodb-core/lib/cursor.js:602:7
        at queryCallback (/home/XXX/api-server/node_modules/mongodb-core/lib/cursor.js:232:18)
        at /home/XXX/api-server/node_modules/mongodb-core/lib/connection/pool.js:469:18
        at process._tickCallback (internal/process/next_tick.js:112:11)

About topology being destroyed and

  console.error node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:176
    Mongoose: users.ensureIndex({ name: 1 }, { background: true })

  console.error node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:176
    Mongoose: users.insert({ XXX })

  console.error node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:176
    Mongoose: users.remove()

  console.error node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:176
    Mongoose: users.ensureIndex({ surname: 1 }, { background: true })

they are all appearing at the same time.

My setup.js looks like this:

import { EventEmitter } from 'events'
import mockgoose from 'mockgoose'
import mongoose from '../src/services/mongoose'
import { mongo } from '../src/config'

EventEmitter.defaultMaxListeners = Infinity
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000

global.Array = Array
global.Date = Date
global.Function = Function
global.Math = Math
global.Number = Number
global.Object = Object
global.RegExp = RegExp
global.String = String
global.Uint8Array = Uint8Array
global.WeakMap = WeakMap
global.Set = Set
global.Error = Error
global.TypeError = TypeError
global.parseInt = parseInt
global.parseFloat = parseFloat

beforeAll(async () => {
  await mockgoose(mongoose)
  mongoose.connect(mongo.uri)
})

afterAll(() => {
  mongoose.disconnect()
})

afterEach(async () => {
  const { collections } = mongoose.connection
  const promises = []
  Object.keys(collections).forEach((collection) => {
    promises.push(collections[collection].remove())
  })
  await Promise.all(promises)
})

which is basically as it was when generated.

/src/services/mongoose.js looks like this:

import Promise from 'bluebird'
import mongoose from 'mongoose'
import { mongo } from '../../config'

Object.keys(mongo.options).forEach((key) => {
  mongoose.set(key, mongo.options[key])
})

mongoose.Promise = Promise
/* istanbul ignore next */
mongoose.Types.ObjectId.prototype.view = function () {
  return { id: this.toString() }
}

/* istanbul ignore next */
mongoose.connection.on('error', (err) => {
  console.error('MongoDB connection error: ' + err)
  process.exit(-1)
})

export default mongoose

I have not changed it at all.

I am having hard time figuring out where to start from in order to fix this, so any help is very welcome! Thanks a lot.

@Martinsos
Copy link
Author

One thing I did do was update jest, to version 22.4.4. But I can't see how could that be causing this issue?

@pedro-victor
Copy link
Collaborator

Mockgoose has been facing issues with parallel testing for some time now. If you run Jest with --runInBand you should stop seeing those topology was destroyed errors.
But our master branch has now migrated to mongodb-memory-server (more details here) and you should the same since running tests serially is soooo 2013 😄

@Martinsos
Copy link
Author

Hi @pedro-victor, thanks a lot for quick answer!
I tried with --runInBand, did not help.
mongodb-memory-server looks really cool, I followed your PR (that helped a lot, thanks!) and replaced mockgoose with it. However, that also did not help!
At the end, I figure out those error messagees (all except topology) are not really error messages, they are "debug" logs from Mongoose which Mongoose is writing to stderr and then Jest is catching them and displaying them like this. I disabled mongoose debugging for tests because I don't really need it, with mongoose.set('debug', false) and that solved the problem.
Regarding topology error, I fiddled a lot but still did not find the cause. However, everything is working and this topology error occurs not always and is not a big bother, so I will close the issue.

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants