Click here to Skip to main content
15,500,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one method which is removing the data from redis and I have to write a unit test for that but when I am mocking the internal method it is always returning zero

What I have tried:

Service.js

const configValues = require('../../config/config');
const constants = require('../../helper/constants');
const asyncRedis = require('async-redis');
const redisClient  = asyncRedis.createClient({
  port: configValues.redisPort,
  host: configValues.redisHost
});
module.exports = {
    clearDataFromRedis: async(userId) => {
      let removeUserDetails=await redisClient.del(userId+'_userDetails');
      return removeUserDetails;
    },
closeRedisInstance :()=>{
        redisClient.quit();
    }
};



Service.test.js

const {clearDataFromRedis ,closeRedisInstance} = require('../..//../src/shared-module/service');
const redis_mock = require("redis-mock");
const client = redis_mock.createClient();

describe('clearDataFromRedis',()=>{
  it('should clear data from redis',async ()=>{
        client.del=jest.fn().mockReturnValue(1);
        const result=await clearDataFromRedis('userData');
        expect(result).toBe(1);    
  })
});
afterAll(() => closeRedisInstance());



after running test it is always returning zero instead of 1
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900