0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-28 18:26:15 +01:00
posthog/jest.setup.redis-mock.js
2020-12-01 20:27:24 +01:00

22 lines
824 B
JavaScript

// Adapted from https://github.com/stipsan/ioredis-mock/issues/568#issuecomment-492558489
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/2ba837f07c0723cde993fb8f791a5fcfdabce719/src/index.js#L100-L109
return {
Command: { _transformer: { argument: {}, reply: {} } },
}
}
// second mock for our code
return function (...args) {
const redis = new Redis(args)
// adapted from copy/paste - our own brpop function!
redis.brpop = async (...args) => {
args.pop()
return [args[0], await redis.rpop(...args)]
}
return redis
}
})