I'm using NestJs with Typeorm, normal setup. UsersService gets the Typeorm Repository injected:
constructor(
@InjectRepository(User)
private usersRepository: Repository<User>,
) {}
In the UsersModule:
@Module({
imports:[ TypeOrmModule.forFeature([User])],
controllers: [UsersController ],
providers: [UsersService]
})
Nothing special as you can see. But the auto-generated test for UsersService fails, no matter what I do:
describe('UsersService', () => {
let service: UsersService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [UsersService],
}).compile();
service = module.get<UsersService>(UsersService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
I get the following error:
Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument UserRepository at index [0] is available in the RootTestModule context.
The solutions on Stackoverflow that I found seem to be obsolete, or over-complicated. I understand that the problem stems from the usage of @InjectRepository.
What is the solution? I tried downloading other people's fairly-similar projects, and get the same error! Both with nest 8 and 7.