williamkapke / mongo-mock

Let's pretend we have a real MongoDB
MIT License
240 stars 74 forks source link

Mongoose mocking #38

Open ksmithut opened 6 years ago

ksmithut commented 6 years ago

Love this project, ran into a hiccup. I thought that since mongoose uses mongodb for its driver, I could mock mongoose with it, but there are some incompatibilities because the default (latest) mongo driver exposes much more than this one does. Example. Mongoose is expecting the Collection class to be available.

I understand if your use case is not to match 100% of the API, that's a tough thing to keep up with, but I applaud the effort. If you don't plan to support it, would you happen to know of alternatives that would mock mongodb or mongoose in-memory?

williamkapke commented 6 years ago

I know there are SOME folks out there doing it- but I don't know the details/success.

Did you checkout #2?

ksmithut commented 6 years ago

I'll take a look and attempt to reproduce. We handle our models a little bit differently, so it'll probably end up being a little bit of a different hacky solution. I'm sure I could get it to work, but it's not something I'd want to maintain long-term. If I find something, I'll post here, or on #2, if I do end up getting something to work

Root-Core commented 6 years ago

I will provide a mongoose driver for mongo-mock as soon as 3.1.0 has landed - as it contains necessary changes - and i cleaned up / tested some things. To be honest, not everything does work as mongo-mock is missing features here and there.. but in a lot of cases, it will be adequate. I'm just a bit short on time right now..

ksmithut commented 6 years ago

No worries :) Thank you for all your hard work on this.

williamkapke commented 6 years ago

@Root-Core I just published 3.1.0!

iliakan commented 6 years ago

So mongoose 5 works well with it now?

iliakan commented 6 years ago

No it doesn't, needs 'connection.js'

angelo-hub commented 6 years ago

@iliakan Can you link an example project putting Mongo-mock in place of the mongodb package for mongoose?

Downchuck commented 5 years ago

This has been a really helpful project to me; seems like global. MONGOOSE_DRIVER_PATH / mongoose.driver.set() compatibility would be a help for these mongoose cases.


const initMongo = (url, debug=true)  =>
  MongoClient.connect(url).then(
    db =>
      new Promise((resolve, reject) => {
        mongoose.set("debug", debug);
        (<any> mongoose).Promise = global.Promise;
        const legacyOpts = { useMongoClient: true, promiseLibrary: global.Promise };
        const legacyUri = "mongodb://localhost:1/fake_db&connectTimeoutMS=1";
        mongoose.connect( legacyUri, legacyOpts ).catch(() => {}).then( () =>mongoose.connection.emit("connected"));
        mongoose.connection.on("error", (e) => console.error("mongoose", e));
        mongoose.connection.on("connected", () => {
          db.serverConfig = { autoReconnect: false };
          const {connection} = mongoose;
          connection.db = db;
          connection.readyState = 1;
          resolve(db);
        });
      })
  );```
gustavohenke commented 3 years ago

It would have been possible to use this module with mongoose... but mongoose disallows classes that don't match the mongodb module.

E.g. this

import { MongoClient } from 'mongo-mock';
import { Connection, Mongoose } from 'mongoose';

beforeEach(async () => {
  const connection = new Connection();
  connection.setClient(await MongoClient.connect("mongodb://localhost:27017/db"));
  const mongoose = new Mongoose();
  mongoose.connections.push(connection);
});

throws this:

MongooseError: Must call setClient() with an instance of MongoClient

LinusU commented 3 years ago

This is what we are currently doing in our tests, to be able to use albatross with mongo-mock:

/* eslint-disable @typescript-eslint/no-var-requires */
require('mongo-mock').max_delay = 0
require('mongodb').MongoClient = require('mongo-mock').MongoClient
require('mongodb').ObjectId = require('mongo-mock').ObjectId
/* eslint-enable @typescript-eslint/no-var-requires */

I imagine it should work for Mongoose as well...