ioredis-mock
ioredis-mock copied to clipboard
zrangebyscore should return a string but returns a number
In the real version of ioredis
zrangebyscore
returns string[]
where as this package returns [string, number]
with the score being a number.
This is a copy of one of my tests that fails because of this.
import { expect, test } from 'vitest';
import { randomUUID } from 'node:crypto';
test('Returns an array of posts', async () => {
const { redis } = await import('@app/common/redis');
const { getFeed } = await import('@app/modules/feed');
// Create a post
const postId = randomUUID();
const createdAt = new Date().getTime();
const updatedAt = new Date().getTime();
await redis.set(`post:${postId}`, JSON.stringify({ createdAt, updatedAt, title: 'TEST POST PLEASE IGNORE' }));
// Add the post to the feed
const followerId = randomUUID();
await redis.zadd(`feed:${followerId}`, createdAt, `post:${postId}`);
// This line fails when using the mock as `createdAt` is a number instead of a string
await expect(redis.zrangebyscore(`feed:${followerId}`, '-inf', '+inf', 'WITHSCORES', 'LIMIT', 0, 100)).resolves.toStrictEqual([`post:${postId}`, `${createdAt}`]);
// The post should be in the feed now
await expect(getFeed(followerId)).resolves.toStrictEqual([`post:${postId}`]);
});
Looking at this I wonder if this is a bug with ioredis and this library is just doing it the "correct" way.
You could write a test for it and see if it behaves the same way in:
npm run test:e2e
as well as npm test
. The npm run test:e2e
suite is running the testing suite on ioredis
against a real instance of Redis instead of ioredis-mock
.
If the two test runs are different then there's a bug in ioredis-mock
.
If they're both wrong, then indeed there's a bug in ioredis
😱
In https://github.com/stipsan/ioredis-mock/pull/1267 , this has been aligned with ioredis
type signatures (string[]
).
I also checked on Redis using Redis Commander and the behavior seems correct:
The last thing to be checked is if ioredis
actually returns strings or performs some kind of casting (I haven't had the chance yet)
Confirmed https://github.com/stipsan/ioredis-mock/commit/d51a3bd7a8d7cb4919107a960fa6eea3feaede33