Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongo-unit

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongo-unit

mongo db for unit tests

  • 1.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
938
decreased by-13.87%
Maintainers
1
Weekly downloads
 
Created
Source

mongo-unit

build status

NPM

I was inspired by dbUnit library, which is very popular in java world.

This library is done to simplify creation of integration tests for node.js application with Mongo DB. I starts local mongodb process using mongodb-prebuilt library, and it work in "InMemory" mode, which improve performance of your tests.

There is alternative library for mocking Mongo: mockgoose

Requirements

It works on Node.js 4+ (ES2015)

Installation

npm install -D mongo-unit

API

start(opts)

It starts mongod on one of available port and returns Promise with URL to connect to this db opts is optional params, you can specify some command line params for mongod (more about it in documentation for mongodb-prebuilt) opts.port - preferable mongo db port, default: 27017 opts.dbName - name of test db, default: test opts.dbpath - db path, default: <node_modules/mongo-unit>\.mongo-unit opts.verbose - enable debug informaton for mongodb-prebuilt, default: false

getUrl()

Syncronius API returns URL to connect to test db, if test DB is not started it thows an Exception

load(data)

Inserts given data (like below) DB collections, returns Promise.

{
  "collectionName1":[
    {"field1":"value1"},
    {"field2":"value2"}
  ],
  "collectionName2":[
    {"field3":"value3"},
    {"field4":"value4"}
  ]
}

clear(data)

Clear collections based on given data (data format is the same), returns Promise.

drop()

Drops test DB, returns Promise.

Basic Usage

Code under test

const mongoose = require('mongoose')
const Schema = mongoose.Schema

function dao(url) {

  mongoose.connect(url)

  const userSchema = new Schema({
    name:  String
  })
  const taskSchema = new Schema({
    userId: String,
    task:  String
  })

  return {
    User:  mongoose.model('user', userSchema),
    Task:  mongoose.model('task', taskSchema)
  }
}

Test data

{
  "users": [
    {
      "_id": "56d9bf92f9be48771d6fe5b1",
      "name": "test"
    },
    {
      "_id": "56d9bf92f9be48771d6fe5b2",
      "name": "John"
    }
  ],
  "tasks": [
    {
      "userId": "56d9bf92f9be48771d6fe5b1",
      "task": "do stuff"
    },
    {
      "userId": "56d9bf92f9be48771d6fe5b1",
      "task": "fix stuff"
    }
  ]
}

Mocha test

describe('dao', ()=>{
  const mongoUnit = require('../index')
  const testData = require('./fixtures/basic.json')
  var daoUT

  before(() => mongoUnit.start()
    .then(url=>daoUT=dao(url))
    .then(()=>mongoUnit.load(testData)))

  after(() => mongoUnit.drop())

  it('should find all users', () => {
    return daoUT.User.find()
      .then(users => {
        expect(users.length).to.equal(2)
        expect(users[0].name).to.equal('test')
      })
  })

  it('should find all tasks for user 1', () => {
    return daoUT.User.find()
      .then(users => users[0])
      .then(user=>daoUT.Task.find({userId: user._id}))
      .then(tasks => {
        expect(tasks.length).to.equal(2)
        expect(tasks[0].task).to.equal('do stuff')
      })
  })
})

Keywords

FAQs

Package last updated on 03 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc