Redis Memory Server

0.16.0 · active · verified Tue Apr 21

redis-memory-server is a utility that programmatically starts a real Redis server instance from Node.js, primarily designed for testing and development mocking. As of version 0.16.0, it provides an isolated, in-memory Redis environment for integration tests, allowing multiple instances to run concurrently on different ports. The package automatically handles downloading and compiling the `redis-server` binary, caching it for subsequent runs to improve performance. It is inspired by `mongodb-memory-server` and differentiates itself by providing a genuine Redis instance rather than a mock, ensuring high fidelity for integration testing. It supports Node.js environments version 18 and above, ships with TypeScript types, and abstracts away the complexities of managing Redis processes for testing workflows. Its release cadence appears active, with consistent updates for features and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to start a RedisMemoryServer instance, connect to it using ioredis, perform basic set/get operations, and ensure proper cleanup.

import { RedisMemoryServer } from 'redis-memory-server';
import { Redis } from 'ioredis'; // ioredis is a common client, install it separately

let redisServer: RedisMemoryServer | undefined;
let redisClient: Redis | undefined;

async function runRedisTest() {
  redisServer = new RedisMemoryServer({
    binary: {
      version: '6.2.6', // Specify a fixed version for deterministic builds
    },
  });

  try {
    const host = await redisServer.getHost();
    const port = await redisServer.getPort();

    console.log(`Redis server started on ${host}:${port}`);

    redisClient = new Redis({
      host,
      port,
    });

    await redisClient.set('mykey', 'myvalue');
    const value = await redisClient.get('mykey');
    console.log(`Retrieved value: ${value}`); // Expected: 'myvalue'

    await redisClient.del('mykey');
    const deleted = await redisClient.get('mykey');
    console.log(`Value after deletion: ${deleted}`); // Expected: null

  } catch (error) {
    console.error('An error occurred during the test:', error);
  } finally {
    if (redisClient) {
      await redisClient.quit();
      console.log('Redis client disconnected.');
    }
    if (redisServer) {
      await redisServer.stop();
      console.log('Redis memory server stopped.');
    }
  }
}

runRedisTest();

view raw JSON →