8

I should run tests on node+express+mongoose+swagger app without mongodb, I need module to mock mongoose (only for tests). I tried mock-mongoose and mockgoose, but I had errors :( Maybe I failed.. or this modules can't help me I hope for your ideas!

I created example skeleton: https://github.com/miroslav-grabinskiy/swagger-server-mock-tested

I need to mock only in tests, and I don't need to test mongoose, like a:

Model.find()

I need test api (routes) like in link

P.S. mock-mongoose not working with promises :(

Jackson
  • 884
  • 2
  • 13
  • 22

3 Answers3

4

I have found the solution - use mockgoose:

"use strict";

const config = require(appRoot + '/config');
const Mongoose = require('mongoose').Mongoose;
const mongoose = new Mongoose();

const DB_NAME = "rbTest";
const DOCKER_MONGODB_URI = "mongodb://mongo:27017/" + DB_NAME;
const LOCAL_MONGODB_URI = "mongodb://localhost:27017/" + DB_NAME + "?socketTimeoutMS=120000";
const myMongo = process.env.DOCKER ? DOCKER_MONGODB_URI : LOCAL_MONGODB_URI;

mongoose.Promise = global.Promise;

if (process.env.NODE_ENV === 'testing') {
  const Mockgoose = require('mockgoose').Mockgoose;
  const mockgoose = new Mockgoose(mongoose);

  mockgoose.prepareStorage().then(function() {
    mongoose.connect(myMongo, function(err) {
      console.log('connected');
    });
  });
} else {
  mongoose.connect(myMongo, config.get('mongoose:options'));
}

module.exports = mongoose;
joshweir
  • 5,427
  • 3
  • 39
  • 59
Jackson
  • 884
  • 2
  • 13
  • 22
  • 4
    Just wanted to add a quick comment here that the current version of Mockgoose (7.3.5) has security vulnerabilities, and that the project appears to be abandoned, not having been touched in eight months. – TheIcemanCometh Sep 24 '18 at 14:53
  • 2
    I can confirm, this project does not work anymore. Currently looking for alternatives, will post here if I find any – DrunkDevKek Nov 15 '18 at 16:27
  • 1
    @DrunkDevKek any alternatives for mongoose mocking ? – asma Aug 13 '20 at 14:38
3

There are several libraries like mockgoose and mockingoose. I have tried several of them, and found a really simple solution, without additional dependencies.

I have a model Foo, which I add to exports in controller file:

//controller.js file
const Foo = mongoose.model('foo_collection', fooSchema);
exports.Foo = Foo;
exports.getFoo = query => Foo.find(query);

And in unit tests (for example, I use Chai), I import the model, and override "find" method:

//test-controller.js file
describe('some test', () => {
    beforeEach(() => {
        Foo.find = async () => [{foo: "bar"}];
    });

    it('should return foo bar', async () => {
        const fooBar = await controller.getFoo();
        assert(fooBar.foo === "bar");
    });
});
Bask.ws
  • 823
  • 4
  • 6
-2

As described in the Jest documentation, I created a __mocks__ folder in my project root directory. Inside mocks folder created a file mongoose.js.

__mocks__/mongoose.js


    module.exports = {
      connect: jest.fn(),
      connection: {
        on: jest.fn(),
        once: jest.fn()
      },
      model: jest.fn(),
      Schema: class Schema {}
    };

Pablo Darde
  • 5,844
  • 10
  • 37
  • 55