Naan and Paneer Makhani

    mock-mongoose
    TypeScript icon, indicating that this package has built-in type declarations

    8.0.1-a • Public • Published

    What is MockMongoose?

    Build Status

    MockMongoose provides test database by spinning up mongod on the back when mongoose.connect call is made. By default it is using in memory store which does not have persistence.

    Install

    To install the latest official version, use NPM:

    npm install mock-mongoose --save-dev

    Usage

    You simply require Mongoose and MockMongoose and wrap Mongoose with MockMongoose.

    var mongoose = require('mongoose');
    var MockMongoose = require('mock-mongoose').MockMongoose;
    var mockMongoose = new MockMongoose(mongoose);
     
    mockMongoose.prepareStorage().then(function() {
        // mongoose connection
    });

    Once Mongoose has been wrapped by MockMongoose connect() will be intercepted by MockMongoose so that no MongoDB instance is created.

    Mocha

    Default mocha timeout is 2000ms, change it to two minutes.

    mocha --timeout 120000

    Same can be done by creating 'mocha.opts' file in your test directory with "--timeout 120000" entry.

    Example

    var Mongoose = require('mongoose').Mongoose;
    var mongoose = new Mongoose();
     
    var MockMongoose = require('mock-mongoose').MockMongoose;
    var mockMongoose = new MockMongoose(mongoose);
     
    before(function(done) {
        mockMongoose.prepareStorage().then(function() {
            mongoose.connect('mongodb://example.com/TestingDB', function(err) {
                done(err);
            });
        });
    });
     
    describe('...', function() {
        it("...", function(done) {
            // ...
            done();
        });
    });

    ES6 Example without mocha

    import * as mongoose from 'mongoose';
    import { MockMongoose } from 'mock-mongoose';
     
    let mockMongoose: MockMongoose = new MockMongoose(mongoose);
     
    mockMongoose.prepareStorage().then(() => {
        mongoose.connect('mongodb://foobar/baz');
        mongoose.connection.on('connected', () => {  
          console.log('db connection is now open');
        }); 
    });

    Helper methods and variables (mockMongoose.helper)

    reset(callback)

    Reset method will remove ALL of the collections from a temporary store, note that this method is part of mock-mongoose object, and not defined under mongoose

    mockMongoose.helper.reset().then(() => {
        done()
    });

    isMocked

    Returns TRUE from mongoose object if MockMongoose is applied

    if ( mockMongoose.helper.isMocked() === true ) {
      // mongoose object is mocked
    }

    setDbVersion(mongoDBVersion: string)

    Set version of MongoDB release

    import * as mongoose from 'mongoose';
    import { MockMongoose } from 'mock-mongoose';
     
    let mockMongoose: MockMongoose = new MockMongoose(mongoose);
    mockMongoose.helper.setDbVersion("3.2.1");
     
    mockMongoose.prepareStorage().then(() => {
        mongoose.connect('mongodb://foobar/baz');
        mongoose.connection.on('connected', () => {  
          console.log('db connection is now open');
        }); 
    });

    setProxy(proxy: string)

    Set proxy for downloading MongoDB release

    import * as mongoose from 'mongoose';
    import { MockMongoose } from 'mock-mongoose';
     
    let mockMongoose: MockMongoose = new MockMongoose(mongoose);
     
    let proxy: string = process.env.http_proxy || 'http://example.com:8080';
    mockMongoose.helper.setProxy(proxy);
     
    mockMongoose.prepareStorage().then(() => {
        mongoose.connect('mongodb://foobar/baz');
        mongoose.connection.on('connected', () => {  
          console.log('db connection is now open');
        }); 
    });

    Development

    This section contains instructions for developers working on the MockMongoose codebase. It is not relevant if you just want to use MockMongoose as a library in your project.

    Pre-requisites

    • Node.js >= 4

    Setup

    git clone git@github.com:CSTLeagueORG/MockMongoose.git
    cd MockMongoose
    npm install
    npm test

    Install

    npm i mock-mongoose

    DownloadsWeekly Downloads

    1,719

    Version

    8.0.1-a

    License

    MIT

    Unpacked Size

    103 kB

    Total Files

    40

    Last publish

    Collaborators

    • polarwooolf