-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error: Nest can't resolve dependencies of the AlertProvider (?). #33
Comments
This issue is because the connection provider is created 'dynamically' when the module's forRootAsync is called. So the provider is created by awaiting an amqp connection and then the provider exists. That's the problem with this module. Having a provider depend on that connection provider sometimes cannot be instanced because the provider it requires cannot be passed in at the time provider being instanced. So to fix this I should probably refactor this package to handle this problem better but I've been struggling for time to dedicate to it plus you can use Transport.RMQ so I think this package is pretty much redundant. In the short term you can fix this by making AlertProvider an AsyncProvider like so @Module({
imports: [
AmqpModule.forFeature(),
],
providers: [
AlertService,
{
provide: AlertProvider,
useFactory: (connection) => new AlertProvider(connection),
inject: ['AMQP_CONNECTION_PROVIDER_default'],
},
],
})
export class AlertModule {} It's a bit of a shit fix but should work. In the future I think I should make a connection's manager and make that the injectable so providers aren't reliant on the connection being created to inject. Plus I should probably make this test better https://github.com/nestjs-community/nestjs-amqp/blob/master/src/__tests__/amqp.module.spec.ts#L103 Perhaps after I finish off the config package's V2 I should do a V2 of this one! |
The fix doesn't work. I still have the same error. I didn't get the point about In any case, thanks for your answer @bashleigh |
I can't use |
Sorry I did start writing a reply but been mega busy! I'll try and make you an example repo and play around with it. I'm going on holiday tomorrow night so I won't be able to reply after then and not before the 21st? I think I come back then? Sorry! I'll do my best! |
Ok, thank you @bashleigh Have a nice holiday! 🎉 |
Sorry I didn't forget just been mega busy! So I tried replicating your code into a test, I updated my nest modules to the latest of latest and I get this same result for all tests now so looks like the package needs an update! I'll have a think as to why the provider tokens are now invalid! |
@bashleigh Nice to hear that you can reproduce an issue! |
Sorry it's been ages... Been mega stressed 😂 isn't life great! So I've taken a quick look and there was only one issues causing the v6 to not pass tests which is odd. If I'm really honest this package could do with a rebuild forbetter implementation, performance and a lot of other things. It's not the best thing in the world. I'll add a test to replicate your issue 👍 |
@probil I just helped someone with a very similar problem, in your test are your providing the following code to your beforeEach(async() => {
const module = await Test.createTestingModule({
providers: [
{
provide: 'AMQP_CONNECTION_PROVIDER_default',
useValue: yourMockObject
}
]
});
}); This is how the package is creating the DI injection token, after looking through the decorator and the token creator method. @bashleigh Do you think it would be possible to expose a method similar to the TypeORM or Mongo methods to get the DI token? It could make for easier testing. Maybe in the rebuild of the package? |
@probil I've just had a thought. You are running @jmcdo29 yea sure I've thought about doing the same thing with the nestjs-config module. I've exported everything in v2 expect that and I needed to use it yesterday. You're welcome to make a PR if you want :) |
I'm new to nest.js. I don't know why Nest cant resolve dependencies. And there is nothing in README. I had found something similar in closed issues but it didn't help. Here is an error
Here is my files (cleaned up a little bit):
Maybe you can suggest me something.
I use
nest@v6
and latest version ofnestjs-amqp
The text was updated successfully, but these errors were encountered: