I'm trying to test my AuthModule controller HTTP layer with supertest as described in the official Nestjs documentation. This module uses an EmailService imported from an EmailModule.
I know you can override providers as follows:
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [
AuthModule,
],
})
.overrideProvider(EmailService)
.useValue(buildMock(EmailService))
but this doesn't work, I'm assuming because the EmailModule is imported in the AuthModule. A workaround is to .overrideProvider(...).useValue(...) every provider in the EmailModule, but this non sense as I'd then have to also mock modules imported by the EmailModule.
When I'm e2e testing the AuthModule, I honestly don't care about how the EmailModule works. All I should need to mock is the EmailService and make sure my auth module interacts with that service properly.
The Test.createTestingModule({}) doesn't have a .overrideModule() method, unfortunately.
I tried mocking the EmailModule with jest:
jest.mock('../../src/email/email.module.ts', () => {
@Module({})
class EmailModule {
}
return EmailModule;
});
but I get this error:
Error: Nest cannot create the module instance. Often, this is because of a circular dependency between modules. Use forwardRef() to avoid it.
(Read more: https://docs.nestjs.com/fundamentals/circular-dependency)
Scope [RootTestModule -> AppModule]
Does anyone know how this can be achieved ?
overrideProviderallows overriding any provider in the context. Thus, ifEmailServiceexists in your app context, it will be overriden. Could you show the@Module()part of theAuthModuleand theEmailModule? - VinceOPS