npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mockgo

v2.0.1

Published

in memory mocking engine for mongodb

Downloads

55

Readme

MOCKGO Build Status

Mockgo is a in-memory mocking engine for mongodb. In contrast to existing solutions mockgo does not try to imitate the mongodb interface by implementing its methods. Instead it uses the same approach Mockgoose uses and spins up an actual mongodb instance which holds the data in memory. That way the full feature set of mongodb can be used.

Works on all platforms which is due to the awesome mongodb-prebuilt package.

After spinning up the mongodb instance a connection to that instance is automatically created and returned.

Installation

npm install mockgo

Usage

Require mockgo, then retrieve a connection to the in-memory instance of mongodb by calling getConnection. An optional database name can be specified.

var mockgo = require('mockgo')

// Using promises
mockgo.getConnection().then(connection => {
  //`connection` is the connection to the mongodb instance
})

// Using callbacks
mockgo.getConnection((error, connection) => {
  //`connection` is the connection to the mongodb instance
})

You can retrieve connections to as many databases as you wish. They are internally cached.

Example

var mockgo = require('mockgo')

// Using promises
return mockgo.getConnection().then(connection => {
  var collection = connection.collection('testDataCollection')
  return collection.find({}).toArray()
    .then(result => console.log(result)) //result: []
    .then(() => collection.insertOne({test: 'data'}))
    .then(() => collection.find({test: 'data'}).toArray())
    .then(result => console.log(result)) //result: [ { _id: 56f52afef6d8838417df1688, test: 'data' } ]
})
.then(() => mockgo.shutDown())
.then(() => console.log('shutdown complete'))

// Using callbacks
mockgo.getConnection('testDatabase', (error, connection) => {
    var collection = connection.collection('testDataCollection')

    collection.find({}).toArray((error, result) => {
        console.log(result); //result: []

        collection.insertOne({test: 'data'}, (error, result) => {
            collection.find({test: 'data'}).toArray((error, result) => {
                console.log(result); //result: [ { _id: 56f52afef6d8838417df1688, test: 'data' } ]

                mockgo.shutDown(() => console.log('shutdown complete'))
            })
        })
    })
})

Methods and Properties

mockgo.getConnection([databaseName])

Returns a promise resolved with a connection to a in-memory instance on mongodb. If no databaseName is specified a dummy name will be used. If a connection to the same database is requested multiple times a cached version of the same connection instance is returned.

The connection instance is a instance from the official native mongodb driver.

If you wish to use another version of the mongodb package you can easily override it by setting the mongodb property before requiring a connection:

var mockgo = require('mockgo')
var mockgo.mongodb = require('mongodb') //version xyz

mockgo.getConnection().then(connection => {
  //`connection` is the connection to the mongodb instance
})

mockgo.getConnection([databaseName], callback)

Same as the promise-based method. Invokes the callback with the connection to the database.

var mockgo = require('mockgo')
var mockgo.mongodb = require('mongodb') //version xyz

mockgo.getConnection((error, connection) => {
    //`connection` is the connection to the mongodb instance
})

mockgo.shutDown()

Closes all existing mongodb connections and shuts down the mongodb instance. Returns a promise resolved once the cleanup has finished.

mockgo.shutDown(callback)

Same as the promise-based method. Invokes the callback once the cleanup has finished

mockgo.mongodb

Exposes the version of the official native mongodb driver, gives the possibility to override it.

Testing with Mocha

This is an example for a simple test with mockgo in mocha. This of course works similar or equally in other test runners, the setup and tearDown semantics may vary.

let expect = require('chai').expect
let mockgo = require('mockgo')

let Loader = require('../lib/Loader')

describe('loaderTest', () => {
    let result, error

    before(done => {
        mockgo.getConnection((error, connection) => {
            expect(error).to.be.null
            loader = new Loader(connection)

            loader.loadSomething((_error, _result) => {
                error = _error
                result = _result
                done()
            })
        })
    })
    after(done => mockgo.shutdown(done))

    it('should not return a error', () => expect(error).to.be.null)
    it('should load something', () => expect(result).to.deep.equal({awesome: 'data'})
})

License

The MIT License (MIT)

Copyright (c) 2016 Manuel Ernst

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.