Skip to content

Example of how to test mongoose middleware without a database connection and/or triggering a model method (save/update etc).

Notifications You must be signed in to change notification settings

vikpe/mongoose-middleware-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mongoose middleware test

Example of how to test mongoose middleware without a database connection and/or triggering a model method (save/update etc).

Problem

  • Mocking model methods (eg doc.save()) prevents registered hooks to be called.

Solution

  1. Expose the middleware function from the Schema.
  2. Rewrite the this context during tests.

Source

(a simple middleware to update the property slug on save)

var mongoose = require('mongoose');
var _kebabCase = require('lodash/kebabCase');

var PostSchema = new mongoose.Schema({
  name: String,
  slug: String,
});

PostSchema._middlewareFunctions = {
  setSlugFromName: function(next) {
    this.slug = _kebabCase(this.name);

    next();
  },
};

PostSchema.pre('save', PostSchema._middlewareFunctions.setSlugFromName);

module.exports = PostSchema;

Test

var expect = require('expect.js');
var sinon = require('sinon');
var _bind = require('lodash/bind');
var PostSchema = require('../src/PostSchema');

describe('Middleware', function() {
  it('should set slug before save', function() {
    var postDoc = {
      name: 'Polly is a Black Parrot',
    };

    var thisContext = postDoc;
    var boundMiddlewareFunc = _bind(PostSchema._middlewareFunctions.setSlugFromName, thisContext);
    var next = sinon.spy();

    boundMiddlewareFunc(next);

    expect(postDoc).to.eql({
      name: 'Polly is a Black Parrot',
      slug: 'polly-is-a-black-parrot',
    });

    sinon.assert.calledOnce(next);
  });
});

Installation / Usage

  1. Clone/download repo
  2. npm install
  3. npm run test

About

Example of how to test mongoose middleware without a database connection and/or triggering a model method (save/update etc).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published