title: Nestjs service test with mock repository not working
body: <p>I am testing Employee service in Nestjs with Typeorm. The service has a injected employees repository. My test strategy is to mock a simple repostiory for testing the service.</p>
  <pre><code>import { Test, TestingModule } from '@nestjs/testing';
  import { getRepositoryToken } from '@nestjs/typeorm';
  import { getRepository, Repository } from 'typeorm';
  import { CreateEmployeeDto } from './dto/create-employee.dto';
  import { Employee } from './employee.entity';
  import { EmployeesService } from './employees.service';
  describe('EmployeesService', () =&gt; {
    let service: EmployeesService;
  
    const mockEmployeesRepository = {
      save: jest.fn().mockImplementation((dto: CreateEmployeeDto) =&gt; {
        return Promise.resolve({
          EmployeeID: Math.ceil(Math.random() * 10),
          ...dto,
        });
      }),
    };
  
    beforeEach(async () =&gt; {
      const module: TestingModule = await Test.createTestingModule({
        providers: [
          EmployeesService,
          {
            provide: getRepositoryToken(Employee),
            useValue: mockEmployeesRepository,
          },
        ],
      }).compile();
  
      service = module.get&lt;EmployeesService&gt;(EmployeesService);
    });
  
    it('should be defined', () =&gt; {
      expect(service).toBeDefined();
    });
  }
  </code></pre>
  <p>But I got <code>EmployeesService › should be defined</code> error. I just cannot figure out why EmployeesService not working in this simple test, even after check <a href="https://stackoverflow.com/questions/55366037/inject-typeorm-repository-into-nestjs-service-for-mock-data-testing">Inject TypeORM repository into NestJS service for mock data testing</a></p>
  <p>more error message:</p>
  <pre><code>EmployeesService › should be defined
  
      expect(received).toBeDefined()
  
      Received: undefined
  
        35 |
        36 |   it('should be defined', () =&gt; {  
      &gt; 37 |     expect(service).toBeDefined(); 
           |                     ^
  </code></pre>
  <p>employees.service.ts</p>
  <pre><code>@Injectable()
  export class EmployeesService {
    constructor(
      @InjectRepository(Employee, 'db1')
      private employeesRepository: Repository&lt;Employee&gt;,
    ) {}
  
    findById(id: number): Promise&lt;Employee&gt; {
      return this.employeesRepository.findOneOrFail(id);
    }
  
    async createEmployee(createEmployeeDto: CreateEmployeeDto) {
      const newEmployee = createEmployeeDto;
      try {
        await this.employeesRepository.save(newEmployee);
      } catch (error) {
        throw new BadRequestException('Cannot create employee');
      }
  
      return newEmployee;
    }
  
    async deleteEmployee(id: number) {
      try {
        return await this.employeesRepository.delete(id);
      } catch (error) {
        throw new BadRequestException('Cannot delete employee');
      }
    }
  
    async updateEmployee(id: number, body: UpdateEmployeeDto) {
      const employee = this.employeesRepository.findOne(id);
      if (!employee) {
        throw new NotFoundException('Employee not found');
      }
  
      const updatedEmployee = await this.employeesRepository.update(id, body);
  
      return this.employeesRepository.findOne(id);
    }
  }
  </code></pre>