liaoliaots/nestjs-redis

ClientNotFoundError: Redis client "default" was not found in the application context.

DavidWz0403 opened this issue · 4 comments

I am currently getting a ClientNofFoundError if I am running my integration tests. However my config and setup seems to be correct as I am just following the example Repo:

redis.config.ts :

export default {
    url: `redis://${process.env.REDIS_USER}:${redisSecret}@${process.env.REDIS_HOSTNAME}:${redisPort}/${process.env.REDIS_OPTIONS}`,
}

app.module.ts :

RedisModule.forRootAsync({
        useFactory: async (configService: ConfigService) => configService.get('redis.config'),
        inject:[ConfigService]
    }),

dummy.repo.ts:

`export class DummyRepository implements IDummyRepository {
  

  constructor(
    @InjectRedis() private readonly redisClient: Redis.Redis,
    private readonly logger: LoggerService
  ) {
    this.logger.setContext(this.constructor.name)
  }`

integration.spec.ts:

const moduleFixture = await Test.createTestingModule({ imports: [AppModule] }).compile();
client = moduleFixture.get<Redis.Redis>(getRedisToken('default'));
 app = moduleFixture.createNestApplication();

Is there still something I am missing ?
As soon as I am trying to run my integration tests, I am getting this error:
image

Thanks in advance !

Could you show me the configuration of ConfigModule?

Yes of course:
ConfigModule.load(path.resolve(__dirname, 'config', '**/!(*.d).{ts,js}')),
I have tried it now with the normal setup instead of the url and it seems to work. However it would be still nice to have the config with the url.´
My config looks now:
export default { // url: redis://${process.env.REDIS_USER}:${redisSecret}@${process.env.REDIS_HOSTNAME}:${redisPort}/${process.env.REDIS_OPTIONS}, host: process.env.REDIS_HOST, port: process.env.REDIS_PORT password: 'process.env.REDIS_PASS }
And there is right now a issue with the building of my nest-js because of the this error export declare const destroy: (clients: ClusterClients) => Promise<[PromiseSettledResult, PromiseSettledResult<"OK">][]>; which seemed to be fixed at your version 5.1.1.
My tsconfig has already "lib": [ "ES2021", "DOM" ], "target": "ES2020",
and my ts version is 4.6.3
Might it be that there is config/dependency issue from my side ?

Also, make sure that the nested structure of your configuration looks like following:

RedisModule.forRootAsync({
    useFactory() {
        return { // <--
            readyLog: true,
            config: {
                url: 'redis://:authpassword@127.0.0.1:6380/4'
            }
        }; // <--
    },
    imports: [ConfigModule],
    inject: [ConfigService]
})

Perfect thanks it works now