Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument UserRepositor

Viewed 13

Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument UserRepository at index [0] is available in the RootTestModule context. Potential solutions: - If UserRepository is a provider, is it part of the current

I'm currently workin in v9 and I have this error, I don't understand why.

Here is my repo: https://github.com/ociel-gonzalez-solis/cv-nestjs-test-portafolio

1 Answers

You never provide a custom provider to mock the UserRepository. Something like

import { Test, TestingModule } from '@nestjs/testing';
import { UsersService } from './users.service';

describe('UsersService', () => {
  let service: UsersService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [
        UsersService,
        {
          provide: getRepositoryToken(User),
          useValue: {
            create: jest.fn(),
            save: jest.fn(),
            find: jest.fn(),
          }
        },
      ],
    }).compile();

    service = module.get<UsersService>(UsersService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });
});

You can then tailor the jest.fn() methods to your liking as your test needs. You can find a whole repository of test examples here

Related