I am working with a Nestjs app that has a module, let's call it ThisModule which has a controller called ControllerForThisModule and a service called ServiceForThisModule where I am injecting a MongoDB model called ModelForThisModule. ThisModule also imports another module called OtherModule where I am injecting a model called ModelInOtherModule. Now, when I run the test, I get the error below?
Nest can't resolve dependencies of the ModelInOtherModule (?). Please make sure that the argument DatabaseConnection at index [0] is available in the MongooseModule context.
Potential solutions:
- If DatabaseConnection is a provider, is it part of the current MongooseModule?
- If DatabaseConnection is exported from a separate @Module, is that module imported within MongooseModule?
@Module({
imports: [ /* the Module containing DatabaseConnection */ ]
This is my code that runs before every test, I am mocking ModelForThisModule using the getModelToken('ModelForThisModule'). However, how do I inject a mock model for ModelInOtherModule for the module OtherModuleWithAModel?
beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [
ServiceForThisModule, {
provide: getModelToken('ModelForThisModule'),
useClass: ModelMock
}
],
imports: [
OtherModuleWithAModel
],
controllers: [ControllerForThisModule],
}).compile();
controller = module.get(B2cDataMealplanFixedSetController);
service = module.get(B2cDataMealplanFixedSetService);
});
Makes sense? or leave a comment if the question is not clear enough