lundi 26 août 2019

Connecting to Mongodb during tests is causing issues

I am using node.js and Mongodb, and running tests with mocha and chai.

Before my tests run, I run a dbsetup:

const server = require('../server')
const db = require('../mongodb')
const ObjectID = require('mongodb').ObjectID

exports.dbSetup = function (cb) {
  if (db.devices === undefined) {
    server.start(() => {
      db.devices.remove({}, function (err, result) {
        if (err) console.log(err)
        db.entries.remove({}, function (err, result) {
          if (err) console.log(err)
          db.actions.remove({}, function (err, result) {
            if (err) console.log(err)
            db.users.remove({}, function (err, result) {
              if (err) console.log(err)
              db.users.update({_id: new ObjectID('5d54490c8a4521464d6bb5af'), email: 't***'}, {$unset: {devices: ''}}, {upsert: true}, function (err, result) {
                if (err) console.log(err)
                db.users.update({email: 't***'}, {$set: {apiKey: '12345'}}, function (err, result) {
                  if (err) console.log(err)
                  db.users.update({email: 't***'}, {$set: {authToken: 'authoToken'}}, function (err, result) {
                    if (err) console.log(err)
                    cb()
                  })
                })
              })
            })
          })
        })
      })
    })
  } else {
    cb()
  }
}

This cleans out the database from the prevous tests, and adds a user used for testing.

I've now added some additional tests, and these require adding more users to the database. Here is my attempt to do this (function is called from same file as above):

exports.pagination = function (cb) {
  if (db.users === undefined) {
    server.start(() => {
      db.users.insert([
        {email: 'abc@gmail.com'},
        {email: 'xyz@aol.net'},
        {email: 'abxy5@yahoo.com'},
        {email: '13579@netscape.org'}
      ], function (err, result) {
        if (err) console.log(err)
      })
    })
    cb()
  }
}

When I do not include the server.start() call, I get an error saying that insertMany isn't a property of undefined, because db.users is coming back undefined. When I do try to the run the server.start() call, I get an error saying "before all" hook: Uncaught Error: listen EADDRINUSE: address already in use :::8080 But my before hook is merely calling the exports.dbSetup shown above:

before(function (done) {
    dbSetup.dbSetup(() => {
      done()
    })
  })

Most of the code in server.start() is standard express-type stuff and middleware, so it shouldn't have any effect on the tests. But here are some parts that may be involved in these errors:

// init database
if (process.env.NODE_ENV !== 'test') {
  mongodb.init((err) => {
    if (err) {
      console.error(err)
      return process.exit(-1)
    }
  })
}

...

if (process.env.NODE_ENV === 'test') {
  app.start = function (done) {
    mongodb.init((err) => {
      if (err) {
        console.error(err)
        return process.exit(-1)
      }
      app.server = http.createServer(app).listen(8080)
      done()
    })
  }

How can I add users to my test database, midway through the test? Is there an easy way to do this that I am missing?

Aucun commentaire:

Enregistrer un commentaire