yeahoffline / redis-mock

Node.js redis client mock
213 stars 111 forks source link

redisMock does not recognize keys set using setEx when testing with jest #193

Open vamsiampolu opened 3 years ago

vamsiampolu commented 3 years ago

I have a key foo that I set to bar to expire in 2 seconds. I'm testing this using redis-mock using jest fake timers. I always get -1 (no expiry associated with key).

Node Version: v14.17.0 redis Version: ^3.0.2 redis-mock Version: ^0.56.3

import { promisify } from 'util';
import * as redis from 'redis-mock';

jest.mock('redis' => redis)

const { redisClient, setEx } = require('./redis');

const ttl = promisify(redisClient.ttl).bind(redisClient);

describe('redisService', () => {
  it('returns the ttl for a key set using setEx', async () => {
       jest.useFakeTimers();
       await setEx('foo', 2, 'bar');
      const sec = await ttl('foo');
      expect(sec).toEqual(2); 
      jest.clearAllTimers();
     jest.useRealTimers();
  })
})