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

mockingoose--temp-fix

v2.8.2

Published

A Jest package for mocking mongoose models

Downloads

10

Readme

Mockingoose

logo

A Jest package for mocking mongoose models

Installation

$ npm i mockingoose -D

Import the library

// using commonJS
const mockingoose = require('mockingoose').default;

// using es201x
import mockingoose from 'mockingoose';

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: '[email protected]'
    };
    
    mockingoose.User.toReturn(_doc, 'findOne'); // findById is findOne
    
    return model
    .findById({ _id: '507f191e810c19729de860ea'})
    .then(doc => {
      expect(JSON.parse(JSON.stringify(doc)).toMatchObject(_doc);
    })
  })
  
  it('should return the doc with update', () => {
      const _doc = {
          _id: '507f191e810c19729de860ea',
          name: 'name',
          email: '[email protected]'
      };
      
      mockingoose.User.toReturn(doc, 'update');
      
      return model
      .update({ name: 'changed' }) // this won't really change anything
      .where({ _id: '507f191e810c19729de860ea'})
      .then(doc => {
        expect(JSON.parse(JSON.stringify(doc)).toMatchObject(_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({ name: '1' });
  mockingoose.User.toReturn({ name: '2' }, 'save');
  
  mockingoose.User.reset(); // will reset all operations;
  mockingoose.User.reset('find'); // will reset only find operations;
})

you can also chain mockingoose#ModelName operations:

mockingoose.User
        .toReturn({ name: 'name' })
        .toReturn({ name: 'a name too' }, 'findOne')
        .toReturn({ name: 'another name' }, 'save')
        .reset('find');

mockingoose#resetAll()

will reset all mocks.

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

Operations available:

  • [x] find - for find query
  • [x] findOne - for findOne query
  • [x] count - for count query
  • [x] distinct - for distinct query
  • [x] findOneAndUpdate - for findOneAndUpdate query
  • [x] findOneAndRemove - for findOneAndRemove query
  • [x] update - for update query
  • [x] save - for create, and save documents Model.create() or Model.save() or doc.save()
  • [x] remove - for remove query
  • [x] deleteOne - for deleteOne query
  • [x] 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.

the returned document is an instance of mongoose Model.

update operation returns original mocked object.

you can simulate Error by passing an Error to mockingoose:

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

return User
    .create({ name: 'name', email: '[email protected]' })
    .catch(err => {
      expect(err.message).toBe('My Error');
    })

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

will work with node 6.4.x. tested with mongoose 4.x and jest 20.x.

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