0

I'm creating some unit tests for my Mongoose models using Lab and Mockgoose, and I'm having the DB connection opened via Labs lab.before() method, then the database connection killed in labs lab.after() method. The problem I'm running into, is if any test cases fail, then the connection won't close, but the lab.after() does get executed (I added some console output for debugging)

const mongoose = require('mongoose')
const mockgoose = require('mockgoose')
const appRoot = require('app-root-path')

mockgoose( mongoose )

// Create the mongoose connection on init
before(function(done) {
    mongoose.connect('mongodb://example.com/TestingDB', function(err) {
        done(err)
    })
})

// Close the fake connection after all tests are done
after(function(done) {
    console.log('Closing') // Shows in console (always)
    mongoose.connection.close(function() {
        console.log('Closed') // Also. always shows in console
        done()
    })
})

const Models = appRoot.require('./dist/models')(mongoose)

describe('Foobar', function () {
    describe('.createFoo', function () {
        it( 'Creating foo with no data', function( done ) {

            // Executing Foobar.createFoo with an empty object WILL populate err
            Models.Foobar.createFoo( { /* Some Data.. */ }, ( err, data ) => {
                // This will fail the test case, since err will contain an error..
                expect( err ).to.not.equal( null )
                done()
            } )
        })
    })
})

So if I change the:

expect( err ).to.not.equal( null )

to:

expect( err ).to.equal( null )

Then it works perfectly fine. But if I simulate a test case failing, then both the "Closing" and "Closed" show up in the console, but the connection itself never closes... It just hangs there until I hit ctrl+c

Am I missing something in the code above?

Update

I looked through the running processes to see if there was a processing hanging when the test case failed:

$ ps aux |grep -i [m]ongo
myuser          3666   0.0  0.5  3054308  19176 s003  S+    2:27PM   0:00.08 node /Users/me/my_project/node_modules/mongodb-prebuilt/binjs/mongokiller.js 3608 3663
Justin
  • 1,959
  • 5
  • 22
  • 40
  • Never used 'to.not.equal' assertion for tests, I would stick 'to.equal' if it works. Also I make and close connection after every test or create a variable that I set to null beforeEach test. – simon-p-r Feb 16 '16 at 21:38

1 Answers1

0

Turns out this was a bug in Mockgoose@5.2.4, upgrading to 5.3.3 solved this (But broke something else :( )

Justin
  • 1,959
  • 5
  • 22
  • 40