Nest can't resolve dependencies: NettJS Unit testing with TypeORM

Viewed 1291

i have following error when try to Unit testing with NestJS.

Nest can't resolve dependencies of the UsersService (UsersRepository, ?, UserRoleRepository, RoleRepository, JWTService). Please make sure that the argument CredentialsRepository at index [1] is available in the RootTestModule context.

test code

  describe("UsersService", () => {
  let service: UsersService;
  let repositoryMock: MockType<Repository<Users>>;
  let model: typeof Users;

  let repo: Repository<Users>;
  let userController: UsersController;
  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [
        TypeOrmModule.forRoot({
          type: "mysql",
          host: config.db.host,
          port: config.db.port,
          username: config.db.username,
          password: config.db.password,
          database: config.db.database,
          autoLoadEntities: true,
          synchronize: true,
        }),
        CredentialsModule,
      ],
      providers: [
        UsersService,
        CredentialsService,
        AuthService,
        JWTService,
        {
          provide: getRepositoryToken(Users),
          // useValue: {
          //   find: jest.fn(() => [Alluser]),
          // },
          useFactory: repositoryMockFactory,
        },
      ],
    }).compile();

    service = module.get<UsersService>(UsersService);
    repositoryMock = module.get(getRepositoryToken(Users));
  });
  it("User details get by Id", async () => {
    repositoryMock.findOne.mockReturnValue(testUser);
    expect(service.findUser(testUser.id)).toEqual(testUser);
    expect(repositoryMock.findOne).toHaveBeenCalledWith(testUser.id);
    // expect(await service.findOne(1)).toBeCalledWith(testUser);
  });

  // it("All Users details ", async () => {
  //   expect(await service.find()).toEqual([Alluser]);
  // });
  // it('Create new User ', async () => {
  //   expect(await service.createNewUser(createUser)).toEqual(testUser);
  // });
});
1 Answers

As per the code, it seems that you are trying to test "UserService", But in the test module, you also have "CredentialsService" which is dependent on the "CredentialsRepository", and is not mocked.

providers: [
    UsersService,
    CredentialsService,
    AuthService,
    JWTService,
    {
      provide: getRepositoryToken(Users),
      // useValue: {
      //   find: jest.fn(() => [Alluser]),
      // },
      useFactory: repositoryMockFactory,
    },
  ],

The basic idea of unit testing is to test one block of the code, and mock the other dependencies of the code, which in this case are "CredentialsService, AuthService, and JWTService".

If the dependencies of the code will not be mocked then the real dependent code will be executed, which will call the further dependencies of the dependent code.

So to avoid those situations, you should mock "CredentialsService, AuthService, and JWTService" services as well, the way you have done for the user service, and then the reported error will be gone.

Firstly declare the variables

let credentialService: CredentialsService;
let authService: AuthService;
let jwtService: JWTService;

And then in the beforeEach block initialize those dependent services

service = module.get<UsersService>(UsersService)
credentialService = module.get<CredentialsService>(CredentialsService)
authService = module.get<AuthService>(AuthService)
jwtService = module.get<JWTService>(JWTService)
Related