title: How Do You Mock EntityManger In TypeORM?
body: <p>I have the following code to mock entityManager from my NestJS service, it works to some extent but the issue comes with specifying what the <code>findOne</code> should return for different entity.</p>
  <pre><code>    {
        provide: getEntityManagerToken(),
        useFactory: (connection: Connection) =&gt; {
          const entityManager = {
            getRepository: jest.fn(() =&gt; {
              return {
                findOne: jest.fn().mockResolvedValue({}),
                save: jest.fn().mockResolvedValue(carData),
                createQueryBuilder: jest.fn(() =&gt; ({
                  select: jest.fn().mockReturnThis(),
                  from: jest.fn().mockReturnThis(),
                  leftJoinAndSelect: jest.fn().mockReturnThis(),
                  where: jest.fn().mockReturnThis(),
                  addGroupBy: jest.fn().mockReturnThis(),
                  groupBy: jest.fn().mockReturnThis(),
                  getOne: jest.fn().mockResolvedValue(''),
                })),
              };
            }),
          };
          return entityManager;
        },
      },
  </code></pre>
  <p>My Service looks like the below code:</p>
  <pre><code>export class CarService {
    constructor(
      @InjectEntityManager()
      private readonly entityManager: EntityManager,
    ) {
      this.carRepository = entityManager.getRepository(Car);
      this.modelRepository = entityManager.getRepository(Model);
     }
      
        const car = await this.carRepository.findOne(data.id);
  
        const model = await this.modelRepository.findOne(data.modelId);
   
  
  </code></pre>
  <p>How do I mock the findOne result from <code>this.carRepository</code> to be different from the findOne result  in <code>this.modelRepository</code> considering that they both will be called within the same service.</p>