nestjs / mongoose

Mongoose module for Nest framework (node.js) 🍸
https://nestjs.com
MIT License
528 stars 118 forks source link

Add mocking possibility to nest/mongoose module #13

Closed MTschannett closed 4 years ago

MTschannett commented 6 years ago

So after opening the issue in the wrong repository, here I'm again.

I'm submitting a...


[ ] Regression 
[ ] Bug report
[*] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Testing e2e with mongoose is done via mockgoose. A connection has to be created, injected as provider (in AppModule) Also models cannot be registered with the mongoose.forFeature()-method, which is very convenient. Im refering to this part of the documentation (https://docs.nestjs.com/recipes/mockgoose)

Expected behavior

It would be nice to get a TestingModule which can be used in e2e tests to mock the mongoose.forRoot()-method. Something like the HttpClientTestingModule from angular. Also it would be great if there would be a way to add fake data, when creating the connection to the mocked database.

Minimal reproduction of the problem with instructions

-

What is the motivation / use case for changing the behavior?

Currently it is cumbersome to setup a mongodb connection (either over mockgoose or a other collection in the database). It would be very convenient to have a built-in way to setup a connection which works together with the other parts of the mongoose package.

Environment

-

codingmaven commented 6 years ago

Any update on this issue?

cloakedch commented 6 years ago

How about using this instead: https://github.com/nodkz/mongodb-memory-server

Looks like mockgoose is abandoned. mongodb-memory-server looks more recent and, imho, mature from what I read in the docs.

MTschannett commented 6 years ago

This sounds like a great option. I guess adding mocked data is also easy.

kamilmysliwiec commented 6 years ago

It should be very easy to use https://github.com/nodkz/mongodb-memory-server together with the existing MongooseModule using Async Configuration feature I believe.

j3ko commented 5 years ago

This works:

@Module({
  imports: [
    MongooseModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => {
        const mongod = new MongoMemoryServer();
        const uri = await mongod.getConnectionString();
        return {
          uri: uri
        }
      },
      inject: [ConfigService],
    }),
  ],
})
export class TestDatabaseModule {}
kamilmysliwiec commented 4 years ago

It seems that the above solution should be sufficient for all use-cases. Thanks for sharing!