ioredis-mock icon indicating copy to clipboard operation
ioredis-mock copied to clipboard

zrangebyscore should return a string but returns a number

Open OmgImAlexis opened this issue 2 years ago • 1 comments

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}`]);
});

OmgImAlexis avatar Jun 17 '22 22:06 OmgImAlexis

Looking at this I wonder if this is a bug with ioredis and this library is just doing it the "correct" way.

OmgImAlexis avatar Jun 17 '22 22:06 OmgImAlexis

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 😱

stipsan avatar Jan 29 '23 12:01 stipsan

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: image

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)

marcoreni avatar Apr 11 '23 13:04 marcoreni

Confirmed https://github.com/stipsan/ioredis-mock/commit/d51a3bd7a8d7cb4919107a960fa6eea3feaede33

stipsan avatar Apr 11 '23 23:04 stipsan