Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created October 20, 2020 17:29
Show Gist options
  • Save sibelius/8c1d25df74ceca6147c43bbe017bea65 to your computer and use it in GitHub Desktop.
Save sibelius/8c1d25df74ceca6147c43bbe017bea65 to your computer and use it in GitHub Desktop.
this is a hacky to make ioredis-mock work well with pubsub - related to https://github.com/stipsan/ioredis-mock/issues/773
jest.mock('ioredis', () => {
const Redis = require('ioredis-mock');
if (typeof Redis === 'object') {
// the first mock is an ioredis shim because ioredis-mock depends on it
// https://github.com/stipsan/ioredis-mock/blob/master/src/index.js#L101-L111
return {
Command: { _transformer: { argument: {}, reply: {} } },
};
}
let instance = null;
// second mock for our code
return function (...args) {
if (instance) {
return instance.createConnectedClient();
}
instance = new Redis(args);
return instance;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment