question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add mocking possibility to nest/mongoose module

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:15
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
j3kocommented, Aug 30, 2019

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 {}
4reactions
cloakedchcommented, Sep 25, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing mongoose models with NestJS - Stack Overflow
I was able to access userModel using module.get like this: userModel = await module.get<User>(getModelToken('User')); Then I was able to mock ...
Read more >
MongoDB | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines ...
Read more >
please make sure that the argument databaseconnection at ...
This is my code that runs before every test, I am mocking ModelForThisModule using the getModelToken('ModelForThisModule'). However, how do I inject a mock...
Read more >
How to test mongoose models with jest and mockingoose
In some cases a solution could be to use an actual test database, so you don't mock anything but you use real data....
Read more >
Using a MongoDB Database in a NestJS Application With ...
Then import MongooseModule from @nest/mongoose: import { MongooseModule } from ... Next, add the following imports to your schema file:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found