You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to mock ioredis so to be able to test it, but unfortunately, amn not able to do it properly, I used also ioredis-mock but it doesnt trigger any event on ready, so it is not useful for our case. So I wonder how do you mock it in your applications?
This is my connection maker, and I would appreciate any help this:
constcreateCacheInstance=async()=>{constaddress=process.env.REDIS_CLUSTER_ADDRESSconstpassword=process.env.REDIS_AUTH_TOKENlogger.info(`Redis cluster env address:${address}`)try{if(!address||!password)thrownewError('Environment variables {REDIS_CLUSTER_ADDRESS or REDIS_AUTH_TOKEN} are not available')consthost=address.split(':')[0]constport=Number(address.split(':')[1])logger.info(`Redis cluster host:${host} port:${port}`)consttlsMode=process.env.NODE_ENV==='production' ? {tls: {}} : {}// https://github.com/redis/ioredis?tab=readme-ov-file#special-note-aws-elasticache-clusters-with-tlsconstredisCluster=newRedis.Cluster([{ host, port }],{slotsRefreshTimeout: 2000,// to prevent [Failed to refresh slots cache error](https://github.com/redis/ioredis/issues/711)scaleReads: 'all',// Send write queries to masters and read queries to masters or slaves randomly.dnsLookup: (address,callback)=>callback(null,address),// https://github.com/redis/ioredis?tab=readme-ov-file#special-note-aws-elasticache-clusters-with-tlsredisOptions: { password, ...tlsMode},})returnawaitnewPromise<{redisCluster?: Cluster;error?: Error}>((resolve,reject)=>{redisCluster.on('connecting',()=>{cacheStatus=redisCluster.statuslogger.info('Connecting to Redis DB')}).on('connect',()=>{cacheStatus=redisCluster.statuslogger.info('Connected to Redis DB')}).on('ready',()=>{cacheStatus=redisCluster.statuslogger.info('Redis cache instance is ready to use')resolve({ redisCluster })}).on('end',()=>{cacheStatus=redisCluster.statuslogger.warn('Redis cache connection has been closed')}).on('error',async(error)=>{cacheStatus=redisCluster.statuscacheError=errorlogger.error(error)try{awaitredisCluster.quit()}catch(e){logger.error({message: 'Error while closing the Redis cache connection', e })}reject({ error })})})}catch(e){consterror=easErrorlogger.error(error)cacheStatus='error'cacheError=errorreturn{ error }}}
What I tried which results in "Connection is closed." "ClusterAllFailedError: Failed to refresh slots cache. which seems I need to mock the connection itself too, but am not sure how?
I'm trying to mock
ioredis
so to be able to test it, but unfortunately, amn not able to do it properly, I used alsoioredis-mock
but it doesnt trigger any eventon ready
, so it is not useful for our case. So I wonder how do you mock it in your applications?This is my connection maker, and I would appreciate any help this:
What I tried which results in
"Connection is closed." "ClusterAllFailedError: Failed to refresh slots cache.
which seems I need to mock the connection itself too, but am not sure how?The text was updated successfully, but these errors were encountered: