Reputation: 57
I want to create a unit test for my NestJS service, which is a database repository.
I have an error: Nest can't resolve dependencies of the RescuerFunctionRepository (?). Please make sure that the argument "RescuerFunctionRepository" at index [0] is available in the RootTestModule context.
.
The code:
describe('RescuerFunctionRepository', () => {
let repository: RescuerFunctionRepository
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [RescuerFunctionRepository],
}).compile()
repository = module.get<RescuerFunctionRepository>(RescuerFunctionRepository)
})
it('should be defined', () => {
expect(repository).toBeDefined()
})
})
It happens also when I add into Test.createTestingModule()
an import statement with my module when I have registered SequelizeModule.
The structure of modules:
forRootAsync
. All of the repositories are registered as providers and are exported.How can I resolve this error?
EDIT: RescuerFunctionRepository code:
import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/sequelize'
import { CreationAttributes, Transaction, WhereOptions } from 'sequelize'
import { RescuerFunction } from '../../models/rescuer-function.model'
@Injectable()
export class RescuerFunctionRepository {
constructor(
@InjectModel(RescuerFunction)
private userModel: typeof RescuerFunction
) {}
find(where: WhereOptions<RescuerFunction>, trans?: Transaction): Promise<RescuerFunction | null> {
return this.userModel.findOne({
where: where,
transaction: trans,
})
}
insert(attributes: CreationAttributes<RescuerFunction>, trans?: Transaction): Promise<RescuerFunction> {
return this.userModel.create(attributes, {
transaction: trans,
})
}
}
Upvotes: 1
Views: 104
Reputation: 57
I know what was wrong.
To the DatabaseModule to exports, I added SequelizeModule
(before this wasn't exported). Added DatabaseModule
and ConfigModule
(with appropriate path to the .env
file) to imports in the test file.
Upvotes: 1
Reputation: 3
Also provide other service which you are using in your service.ts file
Upvotes: 0