GithubHelp home page GithubHelp logo

mongo-mocker's Introduction

Mongo Mocker

A promise/generator driven Mongo mocking layer, with in memory state and mongo operator support.

Install

npm i mongo-mocker --save-dev

Requirements

Node: 6+

Setup

MongoMocker([mockModulePath], initialCollections)

const MongoMocker = require('mongo-mocker');
const mongo = MongoMocker('module/path/for/mongo/driver', { 
    users: [
        { _id: 'xxxx1', name: 'Sam' }, 
        { _id: 'xxxx2', name: 'Claire' }, 
        { _id: 'xxxx3', name: 'Dean' }
    ],
    books: [
        { title: 'Willow' }
    ]
});

/** <==≠ OR ≠===========================> **/

const mongo = require('mongo-mocker')('module/path/for/mongo/driver', {});

/** <==≠ OR (just the mongoDB) ≠========> **/

const mongo = require('mongo-mocker')();

In src code all normal operations happen and work as expected

yield mongo.collection('users').findOne({ name: 'Dean' });
// { _id: 'xxxx3', name: 'Dean' }

Helpers

const mongo = MongoMocker({ users: { _id: '1', name: 'Sam' }, test: [] });

mongo.mock.getCollectionData('users');
// Returns internal state of users collects: [{ _id: '1', name: 'Sam' }]

mongo.mock.clear('test');
// Clear a specific collection

mongo.mock.clarAll();
// Clears all collections

In Tests

const MongoMocker = require('./index');

describe('MongoMocker', () => {
  let mongo;

  before(() => {
    mongo = new MongoMocker('mongoDriver/path', { test: [ { test: -2 } ]});
  });

  afterEach(() => {
    mongo.mock.clearAll();
  });

  it('Inserts and findOne document', function* () {
    const collection = yield mongo.collection('test');
    yield collection.insert({ test: 20 });
    const doc = yield collection.findOne({ test: { $gt: 10 } });
    expect(doc).to.be.a('object');
    expect(doc).to.have.keys(['_id', 'test']);
    expect(doc.test).to.equal(20);
  });

  it('Should init with initial data', function* () {
    mongo = new MongoMocker({ example: [{ object: 1 }, { object: 2 }] });
    expect(mongo.mock.getCollectionData('example')).to.deep.equal([{ object: 1 }, { object: 2 }])
  });
});

mongo-mocker's People

Contributors

labithiotis avatar lexpank avatar

Watchers

James Cloos avatar Matthew Green avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.