Registry / testing / redis-mock

redis-mock

JSON →
library0.56.3jsnpmunverified

Redis client mock object for unit testing. Current stable version is 0.56.3. It provides an in-memory mock of node_redis, allowing developers to test Redis-dependent code without an actual Redis server. Actively maintained but only 46% mock coverage (222/482 commands). Key differentiators: designed as a drop-in replacement for node_redis, supports common operations like strings, hashes, lists, sets, sorted sets, pub/sub, and transactions. However, it lacks complete command coverage and may not support all edge cases.

npm install redis-mock
INSTALL
IMPORT
SIG · REDIS-MOCK
R
redis-mock
testingjavascriptv0.56.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

redis
import redis from 'redis-mock';
import redis from 'redis';
Use redis-mock in tests instead of the real redis package.
createClient
const client = redis.createClient();
const client = new redis.RedisClient();
Use createClient function, not constructor.
client
const { promisify } = require('util'); const getAsync = promisify(client.get).bind(client);
const getAsync = client.getAsync;
No built-in promisify; use util.promisify.

Demonstrates basic usage: create client, set/get a key, cleanup.

const redis = require('redis-mock'); const client = redis.createClient(); client.set('key', 'value', (err, reply) => { if (err) throw err; console.log(reply); // OK }); client.get('key', (err, reply) => { if (err) throw err; console.log(reply); // 'value' }); client.flushall(() => { client.quit(); });
Debug
Known issues
deprecatedPackage is not actively updated; some Redis commands are missing.
fix
Consider using alternatives like ioredis-mock or hand-rolling mocks.
affects: <=0.56.3
gotchaPromises not built-in; must promisify manually.
fix
Use util.promisify or wrap in a Promise.
affects: <=0.56.3
gotchaPub/sub might not work exactly like real Redis; message events may behave differently.
fix
Test thoroughly and consider alternatives for advanced pub/sub scenarios.
affects: <=0.56.3
gotchaSCAN and HSCAN do not support all Redis options like COUNT or MATCH.
fix
Stick to KEYS or use simple patterns.
affects: <=0.56.3
Errors
Common errors & fixes
TypeError: redis.createClient is not a function
Importing 'redis' instead of 'redis-mock' or wrong import style.
fix
Use require('redis-mock') or import redis from 'redis-mock'.
Error: redis_mock is not defined
Using 'redis-mock' as a global variable without proper import.
fix
Install and require/import the package correctly.
TypeError: client.getAsync is not a function
Assuming promisified version exists without using util.promisify.
fix
const { promisify } = require('util'); const getAsync = promisify(client.get).bind(client);
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
Real redis not running and mock not used; mock does not connect to any server.
fix
Mock does not connect; ensure your test setup mocks 'redis' with 'redis-mock'.
Upgrade
Version history
0.56.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
redis-mock — npm install redis-mock · libregistry