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, am not able to do it properly, I triedioredis-mock but it doesnt trigger any event on ready, so it does not work for me or I dont know how to do it properly, please someone shed some light on it
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, am not able to do it properly, I triedioredis-mock
but it doesnt trigger any eventon ready
, so it does not work for me or I dont know how to do it properly, please someone shed some light on itWhat 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: