skip to Main Content

I am trying to write tests for a service that uses ioredis to connect to an elasticache server in AWS. I want to be able to isolate tests from the integration points and want to be able to connect to a redis server locally during the jest tests.

I am able to run setup/teardown of local infrastructure components via `jest. One of the necessary components is redis.

I don’t see any tools to be able to start an in-memory server of redis for your application to then use and connect to via localhost:port.

The only thing I found is https://www.npmjs.com/package/redis-server which is 2 years old. Is this still the recommended tool for this type of job?

I can also try to start a docker container of redis during jest, but don’t see any tools to be able to run the docker container from within jest.

Has anyone been able to solve this problem? The ioredis-mock is not really what I’m looking for as it seems to replace the entire ioredis suite instead of just allowing the ‘real’ ioredis to connect to a redis implementation over tcp (whether it be the real thing or a mock).

2

Answers


  1. You can always drop to the command line, and do what you need there. In the jest setup just run redis as you normally would via the command line, and in teardown do the same.
    Child Process

    Login or Signup to reply.
  2. There is a great project on GitHub, that helps you starting a in-memory redis server for testing (without a password) in your pipeline:
    https://github.com/mhassan1/redis-memory-server.

    If you use and like the project, leave the author a star and contribute if you can.

    If you are using something like vitest or jest, your code could look similar to this:

    import { Redis } from 'ioredis';
    import { RedisMemoryServer } from 'redis-memory-server';
    
    const redisServer = new RedisMemoryServer();
    
    describe("redis tests", () => {
      const redisPort = await redisServer.getHost();
      const redisHost = await redisServer.getPort();
    
      // Tests
      it("connects", async () => {
      // Test code e.g. ioredis
        new Redis({
          port: redisPort,
          host: redisHost ,
        });
      }
      afterAll(async () => {
        await redisServer.stop();
      })
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search