Hey team, a question about testing and DI tree. Given the tested module imports another dynamic module, is there a way to get the instance of the provider exported by that dynamic module?
Here’s my use case:
- I’ve got a module (say
DynamicModule) with aregistermethod that exports a provider (sayExportedService) - I have multiple other modules that import and register the module to use
ExportedService. Something like:
@Module({
imports: DynamicModule.register()
})
class ModuleA
@Module({
imports: DynamicModule.register()
})
class ModuleB
- This means that
ModuleAandModuleBhave different instances ofExportedService
The problem:
I have an E2E test where I need to check ModuleB and get its instance of ExportedService to perform some assertions. The problem is that I cannot find a way of getting that specific instance. I always get ExportedService from ModuleA.
I tried fetching the provider from ModuleB’s DI tree, like below, but then I get Nest could not find ExportedService element (this provider does not exist in the current context)
const app = Test.createTestingModule().compile()
const moduleBContext = app.select(ModuleB);
const serviceInstance = moduleBContext.get(ExportedService, { strict: true });
And without { strict: true } I get the instance from ModuleA. Is there any way around this?