1 import connectionLoader from "../connection"
2
3 const redisConstructorCalls: Array<{ url: string; options: any }> = []
4
5 jest.mock("ioredis", () => {
6 class FakeRedis {
7 constructor(url: string, options: any) {
8 redisConstructorCalls.push({ url, options })
9 }
10 on() {
11 return this
12 }
13 async ping() {
14 return "PONG"
15 }
16 }
17
18 return { __esModule: true, default: FakeRedis }
19 })
20
21 const redisUrl = "redis://localhost:6379"
22
23 const buildArgs = (options: Record<string, unknown>) => {
24 const registered: Record<string, any> = {}
25
26 return {
27 registered,
28 args: {
29 container: {
30 register: (toRegister: Record<string, any>) => {
31 Object.assign(registered, toRegister)
32 },
33 },
34 logger: { info: jest.fn(), warn: jest.fn() },
35 options,
36 } as any,
37 }
38 }
39
40 describe("caching-redis connection loader", () => {
41 beforeEach(() => {
42 redisConstructorCalls.length = 0
43 })
44
45 it("forwards the `redisOptions` object to the ioredis client", async () => {
46 const { args } = buildArgs({ redisUrl, redisOptions: { keepAlive: 10000 } })
47
48 await connectionLoader(args)
49
50 const { options } = redisConstructorCalls[0]
51 expect(options.keepAlive).toEqual(10000)
52 expect(options).not.toHaveProperty("redisOptions")
53 })
54
55 it("does not forward module options to the ioredis client", async () => {
56 const { args } = buildArgs({
57 redisUrl,
58 ttl: 60,
59 prefix: "custom:",
60 compressionThreshold: 512,
61 redisOptions: { keepAlive: 10000 },
62 })
63
64 await connectionLoader(args)
65
66 const { options } = redisConstructorCalls[0]
67 expect(options).not.toHaveProperty("ttl")
68 expect(options).not.toHaveProperty("prefix")
69 expect(options).not.toHaveProperty("compressionThreshold")
70 })
71
72 it("keeps the defaults that `redisOptions` does not override", async () => {
73 const { args } = buildArgs({ redisUrl, redisOptions: { keepAlive: 10000 } })
74
75 await connectionLoader(args)
76
77 const { url, options } = redisConstructorCalls[0]
78 expect(url).toEqual(redisUrl)
79 expect(options).toEqual(
80 expect.objectContaining({
81 connectTimeout: 10000,
82 commandTimeout: 5000,
83 lazyConnect: true,
84 maxRetriesPerRequest: 3,
85 enableOfflineQueue: true,
86 connectionName: "medusa-cache-redis",
87 })
88 )
89 })
90
91 it("lets `redisOptions` override a default", async () => {
92 const { args } = buildArgs({
93 redisUrl,
94 redisOptions: { connectionName: "custom-connection" },
95 })
96
97 await connectionLoader(args)
98
99 expect(redisConstructorCalls[0].options.connectionName).toEqual(
100 "custom-connection"
101 )
102 })
103
104 it("still supports top-level ioredis options, with a deprecation warning", async () => {
105 const { args } = buildArgs({ redisUrl, keepAlive: 10000 })
106
107 await connectionLoader(args)
108
109 expect(redisConstructorCalls[0].options.keepAlive).toEqual(10000)
110 expect(args.logger.warn).toHaveBeenCalledWith(
111 expect.stringContaining("deprecated")
112 )
113 })
114
115 it("does not warn when `redisOptions` is used", async () => {
116 const { args } = buildArgs({ redisUrl, redisOptions: { keepAlive: 10000 } })
117
118 await connectionLoader(args)
119
120 expect(args.logger.warn).not.toHaveBeenCalledWith(
121 expect.stringContaining("deprecated")
122 )
123 })
124
125 it("prefers `redisOptions` over deprecated top-level options", async () => {
126 const { args } = buildArgs({
127 redisUrl,
128 keepAlive: 1,
129 redisOptions: { keepAlive: 10000 },
130 })
131
132 await connectionLoader(args)
133
134 expect(redisConstructorCalls[0].options.keepAlive).toEqual(10000)
135 })
136
137 it("registers the configured prefix", async () => {
138 const { registered, args } = buildArgs({ redisUrl, prefix: "custom:" })
139
140 await connectionLoader(args)
141
142 expect(registered.prefix.resolve()).toEqual("custom:")
143 })
144
145 it("throws when `redisUrl` is missing", async () => {
146 const { args } = buildArgs({ redisOptions: { keepAlive: 10000 } })
147
148 await expect(connectionLoader(args)).rejects.toThrow(
149 "[caching-redis] redisUrl is required"
150 )
151 })
152 })