Skip to content

Commit 7565a36

Browse files
committed
fix: lint and update tests after deps bump
1 parent 994501c commit 7565a36

File tree

4 files changed

+39
-68
lines changed

4 files changed

+39
-68
lines changed

source/lib.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// /source/lib.ts
22
// The redis store code.
33

4-
import {
4+
import type {
55
Store,
66
IncrementResponse,
77
Options as RateLimitConfiguration,
88
} from 'express-rate-limit'
9-
10-
import { Options, SendCommandFn } from './types.js'
9+
import { type Options, type SendCommandFn } from './types.js'
1110

1211
/**
1312
* A `Store` for the `express-rate-limit` package that stores hit counts in
@@ -72,10 +71,10 @@ class RedisStore implements Store {
7271
end
7372
7473
return { totalHits, timeToExpire }
75-
`
74+
`
7675
// Ensure that code changes that affect whitespace do not affect
7776
// the script contents.
78-
.replace(/^\s+/gm, '')
77+
.replaceAll(/^\s+/gm, '')
7978
.trim(),
8079
)
8180

source/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type SendCommandFn = (
1717
/**
1818
* The configuration options for the store.
1919
*/
20-
export interface Options {
20+
export type Options = {
2121
/**
2222
* The function used to send commands to Redis.
2323
*/

test/store-test.ts

Lines changed: 30 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@
22
// The tests for the store.
33

44
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'
119

1210
// The SHA of the script to evaluate
1311
let scriptSha: string | undefined
12+
// The mock redis client to use.
13+
const client = new MockRedisClient()
1414

1515
/**
1616
* A wrapper around the mock redis client to call the right function, as the
1717
* `ioredis-mock` library does not have a send-raw-command function.
1818
*
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.
2120
*
2221
* @return {RedisReply | RedisReply[]} The reply returned by Redis.
2322
*/
2423
const sendCommand = async (
25-
client: Redis,
26-
args: string[],
24+
...args: string[]
2725
): Promise<RedisReply | RedisReply[]> => {
2826
// `SCRIPT LOAD`, called when the store is initialized. This loads the lua script
2927
// for incrementing a client's hit counter.
@@ -54,19 +52,14 @@ const sendCommand = async (
5452

5553
describe('redis store test', () => {
5654
// 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()
6259
})
6360

6461
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-' })
7063
store.init({ windowMs: 10 } as Options)
7164

7265
const key = 'store'
@@ -80,10 +73,7 @@ describe('redis store test', () => {
8073
})
8174

8275
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 })
8777
store.init({ windowMs: 10 } as Options)
8878

8979
const key = 'test-store'
@@ -98,10 +88,7 @@ describe('redis store test', () => {
9888
})
9989

10090
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 })
10592
store.init({ windowMs: 10 } as Options)
10693

10794
const key = 'test-store'
@@ -117,10 +104,7 @@ describe('redis store test', () => {
117104
})
118105

119106
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 })
124108
store.init({ windowMs: 10 } as Options)
125109

126110
const key = 'test-store'
@@ -138,10 +122,7 @@ describe('redis store test', () => {
138122
})
139123

140124
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 })
145126
store.init({ windowMs: 10 } as Options)
146127

147128
const key = 'test-store'
@@ -159,11 +140,7 @@ describe('redis store test', () => {
159140
})
160141

161142
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 })
167144
store.init({ windowMs: 60 } as Options)
168145

169146
const key = 'test-store'
@@ -177,34 +154,26 @@ describe('redis store test', () => {
177154

178155
await store.increment(key) // => 2
179156

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
181158
// `windowMs`).
182159
expect(Number(await client.get('rl:test-store'))).toEqual(2)
183160
expect(Number(await client.pttl('rl:test-store'))).toEqual(60)
184161
})
185162

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)
196166

197-
const keyOne = 'test-store-one'
198-
const keyTwo = 'test-store-two'
167+
const keyOne = 'test-store-one'
168+
const keyTwo = 'test-store-two'
199169

200-
await store.increment(keyOne)
201-
await store.increment(keyTwo)
170+
await store.increment(keyOne)
171+
await store.increment(keyTwo)
202172

203-
Jest.advanceTimersByTime(60)
173+
jest.advanceTimersByTime(60)
204174

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)
209178
})
210179
})

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"include": ["source/"],
33
"exclude": ["node_modules/"],
4-
"extends": "@express-rate-limit/tsconfig"
4+
"extends": "@express-rate-limit/tsconfig",
5+
"compilerOptions": {
6+
"target": "es2021"
7+
}
58
}

0 commit comments

Comments
 (0)