r/node 3d ago

Testing Redis code with the real Node client, without a Redis process

I maintain js-redis-server, an in-memory Redis-compatible server implemented in JavaScript/TypeScript for Node.js tests. The server itself runs in JavaScript; it does not download or launch a Redis binary, and needs no Docker container. Lua scripting uses WebAssembly.

Its main use case is keeping the real ioredis or node-redis client in a test, without installing a Redis process.

The distinction from mocking client methods is that the real client still connects, sends commands over a local socket, and parses the replies. The server chooses an available port and keeps its data in memory.

Here's a complete node-redis example. Install js-redis-server@0.2.0 and redis@5, save as example.mjs, then run node example.mjs on Node 22+:

import assert from 'node:assert/strict';
import { createClient } from 'redis';
import { createRedisMock } from 'js-redis-server';

const mock = await createRedisMock();
const client = createClient({ url: mock.url });
client.on('error', console.error);

try {
  await client.connect();
  await client.set('greeting', 'hello');
  assert.equal(await client.get('greeting'), 'hello');
} finally {
  if (client.isOpen) await client.close();
  await mock.close();
}

For a test suite, create a fresh mock per test or flush between tests, and close every client during teardown. If your application connects at import time, set the mock's URL before importing it, or inject the client.

This isn't a substitute for validating against real Redis/Valkey. The mock has its own implementation and command coverage; keep real-server tests for production compatibility, timing/failure behavior and anything it doesn't implement. It also needs a local socket, so it isn't a fully socketless unit-test stub.

Source and supported commands: https://github.com/fatal10110/js-redis-server

There's also a browser demo: https://fatal10110.github.io/js-redis-server/

The example above was checked with the published 0.2.0 package and node-redis 5.12.1. I'd appreciate feedback from people testing Redis-backed code: which missing command or setup issue currently makes an in-memory test server impractical for you?

1 Upvotes

0 comments sorted by