0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-29 03:04:16 +01:00
posthog/jest.setup.redis-mock.js

22 lines
824 B
JavaScript
Raw Normal View History

2020-12-01 20:27:24 +01:00
// Adapted from https://github.com/stipsan/ioredis-mock/issues/568#issuecomment-492558489
2020-11-30 16:31:38 +01:00
jest.mock('ioredis', () => {
2020-12-01 16:40:53 +01:00
const Redis = require('ioredis-mock')
if (typeof Redis === 'object') {
// the first mock is an ioredis shim because ioredis-mock depends on it
2020-12-01 20:27:24 +01:00
// https://github.com/stipsan/ioredis-mock/blob/2ba837f07c0723cde993fb8f791a5fcfdabce719/src/index.js#L100-L109
2020-12-01 16:40:53 +01:00
return {
Command: { _transformer: { argument: {}, reply: {} } },
}
}
// second mock for our code
return function (...args) {
const redis = new Redis(args)
2020-12-01 20:27:24 +01:00
// adapted from copy/paste - our own brpop function!
2020-12-01 16:40:53 +01:00
redis.brpop = async (...args) => {
args.pop()
return [args[0], await redis.rpop(...args)]
}
return redis
2020-11-30 16:31:38 +01:00
}
})