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

mockingoose

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mockingoose

A Jest package for mocking mongoose models

  • 1.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20K
decreased by-22.61%
Maintainers
1
Weekly downloads
 
Created
Source

Mockingoose

logo

A Jest package for mocking mongoose models

Installation

$ npm i mockingoose -D

Usage

// user.js
import mongoose from 'mongoose';
const { Schema } = mongoose;

const schema = Schema({
    name: String,
    email: String,
    created: { type: Date, default: Date.now }
})

export default mongoose.model('User', schema);
mockingoose#ModelName#toReturn(obj, operation = 'find')
// __tests__/user.test.js
import mockingoose from 'mockingoose';
import model from './model';

describe('test mongoose User model', () => {
  it('should return the doc with findById', () => {
    const doc = {
        _id: '507f191e810c19729de860ea',
        name: 'name',
        email: 'name@email.com'
    };
    
    mockingoose.User.toReturn(doc); // operation `find` is default
    
    return model
    .findById({ _id: '507f191e810c19729de860ea'})
    .then(_doc => {
      expect(_doc).toEqual(doc);
    })
  })
  
  it('should return the doc with update', () => {
      const doc = {
          _id: '507f191e810c19729de860ea',
          name: 'name',
          email: 'name@email.com'
      };
      
      mockingoose.User.toReturn(doc, 'update');
      
      return model
      .update({ name: 'changed' }) // this won't really change anything
      .where({ _id: '507f191e810c19729de860ea'})
      .then(_doc => {
        expect(_doc).toEqual(doc);
      })
    })
})
mockingoose#ModelName#reset(operation = undefined)

will reset Model mock, if pass an operation, will reset only this operation mock.

it('should reset model mock', () => {
  mockingoose.User.toReturn({ test: 1 });
  mockingoose.User.toReturn({ test: 2 }, 'save');
  
  mockingoose.User.reset(); // will reset all operations;
  mockingoose.User.reset('find'); // will reset only find operations;
})
mockingoose#resetAll()

will reset all mocks.

beforeEach(() => {
  mockingoose.resetAll();
})

Operation available:

  • find - for find query
  • findOne - for findOne query
  • count - for count query
  • distinct - for distinct query
  • findOneAndUpdate - for findOneAndUpdate query
  • findOneAndRemove - for findOneAndRemove query
  • update - for update query
  • save - for create, and save documents Model.create() or Model.save() or doc.save()
  • remove - for remove query
  • deleteOne - for deleteOne query
  • deleteMany - for deleteMany query

Notes

All operations works with exec, promise and callback.

if you are using Model.create and you don't pass a mock with mockingoose,
you'll receive the mongoose created doc (with ObjectId and transformations)

validations are working as expected.

you can simulate Error by passing an Error to mockingoose:

mockingoose.User.toReturn(new Error(), 'save');

return User
    .create({ email: 'name@email.com' })
    .catch(err => {
      expect(err).toBeInstanceOf(Error);
    })

no connection is made to the database (mongoose.connect is jest.fn())

check tests for more, feel free to fork and contribute.

Keywords

FAQs

Package last updated on 09 Jul 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