How to mock the ioredis get set in jest testcase .
my api is as given below:-
const Redis = require('ioredis');
const redisClient = new Redis();
router.get('/qms', async (req, res, next) => {
let data = await redisClient.get('qms');
console.log(data)
}
})
my jest testcase is as given below:-
const Redis = require('ioredis');
let redisClient = new Redis();
jest.mock('ioredis', () => {
return {
__esModule: true,
default: jest.fn(() => redisClient), // Mock the creation of a new Redis client
};
});
describe('QMS get API tests', () => {
it('should return QMS data if available', async () => {
// await redisClient.set('qms', JSON.stringify({ date: new Date(), description: 'Server token description' }));
// await redisClient.get('qms');
redisClient.get.mockResolvedValue(JSON.stringify({ date: new Date(), description: 'Server token description' }));
const response = await request(app).get('/api/automation/qms');
expect(response.status).toBe(200);
// expect(response.body).toEqual({ status: false, description: 'Not started yet' });
});
});
but when i have written above testcase , it is not mocking in the api properly in await redisClient.get('qms');
i am also doing console.log(data) , but it is giving undefined .
please suggest the updated code for testcase
i have also tried ioredis-mock , but it is working in testcase only but in api it is logging undefined only .