2
2
// The tests for the store.
3
3
4
4
import { createHash } from 'node:crypto'
5
-
6
- import { jest as Jest } from '@jest/globals'
7
- import { Options } from 'express-rate-limit'
8
- import MockRedisClient , { Redis } from 'ioredis-mock'
9
-
10
- import RedisStore , { RedisReply } from '../source/index.js'
5
+ import { jest } from '@jest/globals'
6
+ import { type Options } from 'express-rate-limit'
7
+ import MockRedisClient from 'ioredis-mock'
8
+ import RedisStore , { type RedisReply } from '../source/index.js'
11
9
12
10
// The SHA of the script to evaluate
13
11
let scriptSha : string | undefined
12
+ // The mock redis client to use.
13
+ const client = new MockRedisClient ( )
14
14
15
15
/**
16
16
* A wrapper around the mock redis client to call the right function, as the
17
17
* `ioredis-mock` library does not have a send-raw-command function.
18
18
*
19
- * @param {Redis } client - The mock Redis client.
20
- * @param {string[] } args - The raw command to send.
19
+ * @param {string[] } ...args - The raw command to send.
21
20
*
22
21
* @return {RedisReply | RedisReply[] } The reply returned by Redis.
23
22
*/
24
23
const sendCommand = async (
25
- client : Redis ,
26
- args : string [ ] ,
24
+ ...args : string [ ]
27
25
) : Promise < RedisReply | RedisReply [ ] > => {
28
26
// `SCRIPT LOAD`, called when the store is initialized. This loads the lua script
29
27
// for incrementing a client's hit counter.
@@ -54,19 +52,14 @@ const sendCommand = async (
54
52
55
53
describe ( 'redis store test' , ( ) => {
56
54
// Mock timers so we can fast forward time instead of waiting for n seconds
57
- // in the timer section
58
- beforeEach ( ( ) => Jest . useFakeTimers ( 'modern' ) )
59
- afterEach ( ( ) => {
60
- Jest . useRealTimers ( )
61
- Jest . restoreAllMocks ( )
55
+ beforeEach ( ( ) => jest . useFakeTimers ( ) )
56
+ afterEach ( async ( ) => {
57
+ jest . useRealTimers ( )
58
+ await client . flushall ( )
62
59
} )
63
60
64
61
it ( 'supports custom prefixes' , async ( ) => {
65
- const client = new MockRedisClient ( )
66
- const store = new RedisStore ( {
67
- sendCommand : async ( ...args : string [ ] ) => sendCommand ( client , args ) ,
68
- prefix : 'test-' ,
69
- } )
62
+ const store = new RedisStore ( { sendCommand, prefix : 'test-' } )
70
63
store . init ( { windowMs : 10 } as Options )
71
64
72
65
const key = 'store'
@@ -80,10 +73,7 @@ describe('redis store test', () => {
80
73
} )
81
74
82
75
it ( 'sets the value to 1 on first call to `increment`' , async ( ) => {
83
- const client = new MockRedisClient ( )
84
- const store = new RedisStore ( {
85
- sendCommand : async ( ...args : string [ ] ) => sendCommand ( client , args ) ,
86
- } )
76
+ const store = new RedisStore ( { sendCommand } )
87
77
store . init ( { windowMs : 10 } as Options )
88
78
89
79
const key = 'test-store'
@@ -98,10 +88,7 @@ describe('redis store test', () => {
98
88
} )
99
89
100
90
it ( 'increments the key for the store when `increment` is called' , async ( ) => {
101
- const client = new MockRedisClient ( )
102
- const store = new RedisStore ( {
103
- sendCommand : async ( ...args : string [ ] ) => sendCommand ( client , args ) ,
104
- } )
91
+ const store = new RedisStore ( { sendCommand } )
105
92
store . init ( { windowMs : 10 } as Options )
106
93
107
94
const key = 'test-store'
@@ -117,10 +104,7 @@ describe('redis store test', () => {
117
104
} )
118
105
119
106
it ( 'decrements the key for the store when `decrement` is called' , async ( ) => {
120
- const client = new MockRedisClient ( )
121
- const store = new RedisStore ( {
122
- sendCommand : async ( ...args : string [ ] ) => sendCommand ( client , args ) ,
123
- } )
107
+ const store = new RedisStore ( { sendCommand } )
124
108
store . init ( { windowMs : 10 } as Options )
125
109
126
110
const key = 'test-store'
@@ -138,10 +122,7 @@ describe('redis store test', () => {
138
122
} )
139
123
140
124
it ( 'resets the count for a key in the store when `resetKey` is called' , async ( ) => {
141
- const client = new MockRedisClient ( )
142
- const store = new RedisStore ( {
143
- sendCommand : async ( ...args : string [ ] ) => sendCommand ( client , args ) ,
144
- } )
125
+ const store = new RedisStore ( { sendCommand } )
145
126
store . init ( { windowMs : 10 } as Options )
146
127
147
128
const key = 'test-store'
@@ -159,11 +140,7 @@ describe('redis store test', () => {
159
140
} )
160
141
161
142
it ( 'resets expiry time on change if `resetExpiryOnChange` is set to `true`' , async ( ) => {
162
- const client = new MockRedisClient ( )
163
- const store = new RedisStore ( {
164
- sendCommand : async ( ...args : string [ ] ) => sendCommand ( client , args ) ,
165
- resetExpiryOnChange : true ,
166
- } )
143
+ const store = new RedisStore ( { sendCommand, resetExpiryOnChange : true } )
167
144
store . init ( { windowMs : 60 } as Options )
168
145
169
146
const key = 'test-store'
@@ -177,34 +154,26 @@ describe('redis store test', () => {
177
154
178
155
await store . increment ( key ) // => 2
179
156
180
- // Ensure the hit count is 1 , and the expiry is 60 milliseconds (value of
157
+ // Ensure the hit count is 2 , and the expiry is 60 milliseconds (value of
181
158
// `windowMs`).
182
159
expect ( Number ( await client . get ( 'rl:test-store' ) ) ) . toEqual ( 2 )
183
160
expect ( Number ( await client . pttl ( 'rl:test-store' ) ) ) . toEqual ( 60 )
184
161
} )
185
162
186
- describe ( 'reset time' , ( ) => {
187
- beforeEach ( ( ) => Jest . useFakeTimers ( 'modern' ) )
188
- afterEach ( ( ) => Jest . useRealTimers ( ) )
189
-
190
- it ( 'resets the count for all the keys in the store when the timeout is reached' , async ( ) => {
191
- const client = new MockRedisClient ( )
192
- const store = new RedisStore ( {
193
- sendCommand : async ( ...args : string [ ] ) => sendCommand ( client , args ) ,
194
- } )
195
- store . init ( { windowMs : 50 } as Options )
163
+ it ( 'resets the count for all the keys in the store when the timeout is reached' , async ( ) => {
164
+ const store = new RedisStore ( { sendCommand } )
165
+ store . init ( { windowMs : 50 } as Options )
196
166
197
- const keyOne = 'test-store-one'
198
- const keyTwo = 'test-store-two'
167
+ const keyOne = 'test-store-one'
168
+ const keyTwo = 'test-store-two'
199
169
200
- await store . increment ( keyOne )
201
- await store . increment ( keyTwo )
170
+ await store . increment ( keyOne )
171
+ await store . increment ( keyTwo )
202
172
203
- Jest . advanceTimersByTime ( 60 )
173
+ jest . advanceTimersByTime ( 60 )
204
174
205
- // Ensure that the keys have been deleted
206
- expect ( await client . get ( 'rl:test-store-one' ) ) . toEqual ( null )
207
- expect ( await client . get ( 'rl:test-store-two' ) ) . toEqual ( null )
208
- } )
175
+ // Ensure that the keys have been deleted
176
+ expect ( await client . get ( 'rl:test-store-one' ) ) . toEqual ( null )
177
+ expect ( await client . get ( 'rl:test-store-two' ) ) . toEqual ( null )
209
178
} )
210
179
} )
0 commit comments