title: How to test Models (Mongoose) in a service (NestJS) with jest
body: <p>I have a backend done with NestJS. In my service I inject two Mongoose Models. I use Jest to test the service.
  Models are declared as is and injected into the module:</p>
  
  <pre><code>quizes.providers.ts
  import { Connection } from 'mongoose';
  import { QuizSchema } from './schemas/quiz.schema';
  
  export const quizesProviders = [
    {
      provide: 'CLASS_MODEL',
      useFactory: (connection: Connection) =&gt; connection.model('Quiz', QuizSchema),
      inject: ['DATABASE_CONNECTION'],
    },
  ];
  
  users.providers.ts
  import { Connection } from 'mongoose';
  import { UserSchema } from './schemas/user.schema';
  
  export const usersProviders = [
    {
      provide: 'USER_MODEL',
      useFactory: (connection: Connection) =&gt; connection.model('User', UserSchema),
      inject: ['DATABASE_CONNECTION'],
    },
  ];
  </code></pre>
  
  <p>Example of module:</p>
  
  <pre><code>quizes.module.ts
  import { Module } from '@nestjs/common';
  import { QuizesController } from './quizes.controller';
  import { QuizesService } from './quizes.service';
  
  import { quizesProviders } from './quizes.providers';
  
  import { usersProviders } from '../auth/users.providers';
  
  import { DatabaseModule } from 'src/database.module';
  import { AuthModule } from 'src/auth/auth.module';
  
  @Module({
    imports: [DatabaseModule, AuthModule],
    controllers: [QuizesController],
    providers: [QuizesService,
    ...quizesProviders, ...usersProviders]
  })
  export class QuizesModule {}
  </code></pre>
  
  <p>Then in my service, I inject models:</p>
  
  <pre><code>quizes.service.ts
  @Injectable()
  export class QuizesService {
      constructor(
          @Inject('CLASS_MODEL')
          private classModel: Model&lt;Quiz&gt;,
          @Inject('USER_MODEL')
          private userModel: Model&lt;User&gt;
        ) {}
  </code></pre>
  
  <p>In my quizes.spec.ts (jest) I began to do things like that. It compiles but doesn't work:</p>
  
  <pre><code>import { Test } from '@nestjs/testing';
  import * as mongoose from 'mongoose';
  import { User } from 'src/auth/user.interface';
  import { Quiz } from './quiz.interface';
  import { databaseProviders } from '../database.providers';
  
  const USER_MODEL:mongoose.Model&lt;User&gt; =  mongoose.model('User', UserSchema);
  
  const CLASS_MODEL:mongoose.Model&lt;Quiz&gt; =  mongoose.model('Quiz', QuizSchema);
  
  const mockingQuizModel = () =&gt; {
    find: jest.fn()
  }
  
  const mockingUserModel = () =&gt; {
   find: jest.fn()
  }
  
  const mockUser = {
    username: 'Test user'
  }
  
  describe('QuizesService', () =&gt; {
      let quizesService;
      let userModel , classModel;
  
  
      beforeEach(async () =&gt; {
          const module = await Test.createTestingModule({
  
              providers: [QuizesService, ...usersProviders, ...quizesProviders,...databaseProviders,
              {provide: USER_MODEL, useFactory: mockingUserModel},
              {provide: CLASS_MODEL, useFactory: mockingQuizModel},
          ],
          }).compile();
  
          quizesService = await module.get&lt;QuizesService&gt;(QuizesService);
          classModel = await module.get&lt;mongoose.Model&lt;Quiz&gt;&gt;(CLASS_MODEL)
          userModel = await module.get&lt;mongoose.Model&lt;User&gt;&gt;(USER_MODEL)
  
      })
  
      describe('getAllQuizes', ()=&gt; {
  
          it('get all quizes', () =&gt; {
             expect(userModel.find).not.toHaveBeenCalled();
          })
  
      })
  })
  </code></pre>
  
  <p><code>userModel</code> is undefined and the test does not exit.</p>